{ "cells": [ { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "100\n", "Original Hash: 4adcfc6bb9af0e6eada5d4c212c29b100d58c832a641bff902d931f061e4a32b\n", "Modified Hash: c978ccaa0f98f616d249d7ad78f4c5c4534b26e9bc32b6a644e278159bf2a93e\n" ] } ], "source": [ "from safetensors import safe_open\n", "from safetensors.torch import save_file\n", "import torch\n", "import hashlib\n", "import random\n", "\n", "filename = \"model-00003-of-00004.safetensors\"\n", "modified_model_name = \"modified_model3.safetensors\"\n", "\n", "\n", "with safe_open(filename, framework=\"pt\") as f:\n", " tensors = {key: f.get_tensor(key) for key in f.keys()}\n", "\n", "def introduce_noise(tensor, noise_level=1e-8):\n", " noise = torch.randn(tensor.size()) * noise_level\n", " return (tensor + noise).to(tensor.dtype)\n", "modified = 0;\n", "for key in tensors:\n", " modified +=1\n", " tensors[key] = introduce_noise(tensors[key])\n", "print(modified)\n", "\n", "metadata = {\n", "\"format\": \"pt\" # Adjust based on actual format needed\n", "}\n", "\n", "save_file(tensors, modified_model_name, metadata=metadata)\n", "\n", "\n", "def compute_hash(filename):\n", " hasher = hashlib.sha256()\n", " with open(filename, \"rb\") as f:\n", " buf = f.read()\n", " hasher.update(buf)\n", " return hasher.hexdigest()\n", "\n", "original_hash = compute_hash(filename)\n", "modified_hash = compute_hash(modified_model_name)\n", "\n", "print(f\"Original Hash: {original_hash}\")\n", "print(f\"Modified Hash: {modified_hash}\")" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "104\n" ] }, { "ename": "KeyboardInterrupt", "evalue": "", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)", "Cell \u001b[0;32mIn[7], line 27\u001b[0m\n\u001b[1;32m 21\u001b[0m \u001b[39mprint\u001b[39m(modified)\n\u001b[1;32m 23\u001b[0m metadata \u001b[39m=\u001b[39m {\n\u001b[1;32m 24\u001b[0m \u001b[39m\"\u001b[39m\u001b[39mformat\u001b[39m\u001b[39m\"\u001b[39m: \u001b[39m\"\u001b[39m\u001b[39mpt\u001b[39m\u001b[39m\"\u001b[39m \u001b[39m# Adjust based on actual format needed\u001b[39;00m\n\u001b[1;32m 25\u001b[0m }\n\u001b[0;32m---> 27\u001b[0m save_file(tensors, modified_model_name, metadata\u001b[39m=\u001b[39;49mmetadata)\n\u001b[1;32m 30\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mcompute_hash\u001b[39m(filename):\n\u001b[1;32m 31\u001b[0m hasher \u001b[39m=\u001b[39m hashlib\u001b[39m.\u001b[39msha256()\n", "File \u001b[0;32m/opt/miniconda3/envs/bittensor/lib/python3.10/site-packages/safetensors/torch.py:286\u001b[0m, in \u001b[0;36msave_file\u001b[0;34m(tensors, filename, metadata)\u001b[0m\n\u001b[1;32m 255\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39msave_file\u001b[39m(\n\u001b[1;32m 256\u001b[0m tensors: Dict[\u001b[39mstr\u001b[39m, torch\u001b[39m.\u001b[39mTensor],\n\u001b[1;32m 257\u001b[0m filename: Union[\u001b[39mstr\u001b[39m, os\u001b[39m.\u001b[39mPathLike],\n\u001b[1;32m 258\u001b[0m metadata: Optional[Dict[\u001b[39mstr\u001b[39m, \u001b[39mstr\u001b[39m]] \u001b[39m=\u001b[39m \u001b[39mNone\u001b[39;00m,\n\u001b[1;32m 259\u001b[0m ):\n\u001b[1;32m 260\u001b[0m \u001b[39m \u001b[39m\u001b[39m\"\"\"\u001b[39;00m\n\u001b[1;32m 261\u001b[0m \u001b[39m Saves a dictionary of tensors into raw bytes in safetensors format.\u001b[39;00m\n\u001b[1;32m 262\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 284\u001b[0m \u001b[39m ```\u001b[39;00m\n\u001b[1;32m 285\u001b[0m \u001b[39m \"\"\"\u001b[39;00m\n\u001b[0;32m--> 286\u001b[0m serialize_file(_flatten(tensors), filename, metadata\u001b[39m=\u001b[39;49mmetadata)\n", "\u001b[0;31mKeyboardInterrupt\u001b[0m: " ] } ], "source": [ "from safetensors import safe_open\n", "from safetensors.torch import save_file\n", "import torch\n", "import hashlib\n", "import random\n", "\n", "filename = \"model-00002-of-00004.safetensors\"\n", "modified_model_name = \"modified_model2.safetensors\"\n", "\n", "\n", "with safe_open(filename, framework=\"pt\") as f:\n", " tensors = {key: f.get_tensor(key) for key in f.keys()}\n", "\n", "def introduce_noise(tensor, noise_level=1e-8):\n", " noise = torch.randn(tensor.size()) * noise_level\n", " return (tensor + noise).to(tensor.dtype)\n", "modified = 0;\n", "for key in tensors:\n", " modified +=1\n", " tensors[key] = introduce_noise(tensors[key])\n", "print(modified)\n", "\n", "metadata = {\n", "\"format\": \"pt\" # Adjust based on actual format needed\n", "}\n", "\n", "save_file(tensors, modified_model_name, metadata=metadata)\n", "\n", "\n", "def compute_hash(filename):\n", " hasher = hashlib.sha256()\n", " with open(filename, \"rb\") as f:\n", " buf = f.read()\n", " hasher.update(buf)\n", " return hasher.hexdigest()\n", "\n", "original_hash = compute_hash(filename)\n", "modified_hash = compute_hash(modified_model_name)\n", "\n", "print(f\"Original Hash: {original_hash}\")\n", "print(f\"Modified Hash: {modified_hash}\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "from safetensors import safe_open\n", "from safetensors.torch import save_file\n", "import hashlib\n", "\n", "filename = \"model-00002-of-00004.safetensors\"\n", "modified_model_name = \"modified_model2.safetensors\"\n", "\n", "# Load the original model\n", "with safe_open(filename, framework=\"pt\") as f:\n", " tensors = {key: f.get_tensor(key) for key in f.keys()}\n", "\n", "# Define anonymous metadata\n", "metadata = {\n", " \"description\": \"This model has anonymous metadata\", # Neutral description\n", " \"created_by\": \"anonymous\", # Anonymous creator\n", " \"format\": \"pt\" # Adjust based on actual format needed\n", "}\n", "\n", "# Save the model with new metadata but without altering weights\n", "save_file(tensors, modified_model_name, metadata=metadata)\n", "\n", "# Function to compute file hash\n", "def compute_hash(filename):\n", " hasher = hashlib.sha256()\n", " with open(filename, \"rb\") as f:\n", " buf = f.read()\n", " hasher.update(buf)\n", " return hasher.hexdigest()\n", "\n", "# Compute hashes\n", "original_hash = compute_hash(filename)\n", "modified_hash = compute_hash(modified_model_name)\n", "\n", "# Output hash values\n", "print(f\"Original Hash: {original_hash}\")\n", "print(f\"Modified Hash: {modified_hash}\")" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Original Hash: cf348c92be8c3249369823bd5261607e9cd3197cf67214addf87578b8a433fdf\n", "Modified Hash: 739d647be45dcab08339cda63405c0c6acf9cfdc0510d1fb08529810cf1c0672\n" ] } ], "source": [ "from safetensors import safe_open\n", "from safetensors.torch import save_file\n", "import hashlib\n", "\n", "filename = \"model-00004-of-00004.safetensors\"\n", "modified_model_name = \"modified_model4.safetensors\"\n", "\n", "# Load the original model\n", "with safe_open(filename, framework=\"pt\") as f:\n", " tensors = {key: f.get_tensor(key) for key in f.keys()}\n", "\n", "# Define anonymous metadata\n", "metadata = {\n", " \"description\": \"This model has anonymous metadata\", # Neutral description\n", " \"created_by\": \"anonymous\", # Anonymous creator\n", " \"format\": \"pt\" # Adjust based on actual format needed\n", "}\n", "\n", "# Save the model with new metadata but without altering weights\n", "save_file(tensors, modified_model_name, metadata=metadata)\n", "\n", "# Function to compute file hash\n", "def compute_hash(filename):\n", " hasher = hashlib.sha256()\n", " with open(filename, \"rb\") as f:\n", " buf = f.read()\n", " hasher.update(buf)\n", " return hasher.hexdigest()\n", "\n", "# Compute hashes\n", "original_hash = compute_hash(filename)\n", "modified_hash = compute_hash(modified_model_name)\n", "\n", "# Output hash values\n", "print(f\"Original Hash: {original_hash}\")\n", "print(f\"Modified Hash: {modified_hash}\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }