Delete modifyhash.py
Browse files- modifyhash.py +0 -52
modifyhash.py
DELETED
|
@@ -1,52 +0,0 @@
|
|
| 1 |
-
from safetensors import safe_open
|
| 2 |
-
from safetensors.torch import save_file
|
| 3 |
-
import torch
|
| 4 |
-
import hashlib
|
| 5 |
-
import random
|
| 6 |
-
import os
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def introduce_noise(tensor, noise_level=1e-8):
|
| 10 |
-
noise = torch.randn(tensor.size()) * noise_level
|
| 11 |
-
return (tensor + noise).to(tensor.dtype)
|
| 12 |
-
|
| 13 |
-
for i in range(1,6):
|
| 14 |
-
tensors=None;
|
| 15 |
-
filename = f"model-0000{i}-of-00005.safetensors"
|
| 16 |
-
modified_model_name = f"modified_model{i}.safetensors"
|
| 17 |
-
|
| 18 |
-
with safe_open(filename, framework="pt") as f:
|
| 19 |
-
tensors = {key: f.get_tensor(key) for key in f.keys()}
|
| 20 |
-
modified = 0;
|
| 21 |
-
|
| 22 |
-
for key in tensors:
|
| 23 |
-
modified +=1
|
| 24 |
-
tensors[key] = introduce_noise(tensors[key])
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
print(modified)
|
| 29 |
-
|
| 30 |
-
metadata = {
|
| 31 |
-
"format": "pt" # Adjust based on actual format needed
|
| 32 |
-
}
|
| 33 |
-
if os.path.exists(filename):
|
| 34 |
-
os.remove(filename)
|
| 35 |
-
save_file(tensors, filename, metadata=metadata)
|
| 36 |
-
else:
|
| 37 |
-
print("file does not exist")
|
| 38 |
-
save_file(tensors, modified_model_name, metadata=metadata)
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
# def compute_hash(filename):
|
| 42 |
-
# hasher = hashlib.sha256()
|
| 43 |
-
# with open(filename, "rb") as f:
|
| 44 |
-
# buf = f.read()
|
| 45 |
-
# hasher.update(buf)
|
| 46 |
-
# return hasher.hexdigest()
|
| 47 |
-
|
| 48 |
-
# original_hash = compute_hash(filename)
|
| 49 |
-
# modified_hash = compute_hash(modified_model_name)
|
| 50 |
-
|
| 51 |
-
# print(f"Original Hash: {original_hash}")
|
| 52 |
-
# print(f"Modified Hash: {modified_hash}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|