Create rp.py
Browse files
rp.py
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import load_file, save_file
|
| 3 |
+
|
| 4 |
+
def reduce_key_size(input_file, output_file, reduction_factor=0.30):
|
| 5 |
+
# Load the model
|
| 6 |
+
model_data = load_file(input_file)
|
| 7 |
+
|
| 8 |
+
# Iterate through all the tensors and reduce their size
|
| 9 |
+
for key in model_data.keys():
|
| 10 |
+
original_tensor = model_data[key]
|
| 11 |
+
|
| 12 |
+
# Calculate the new size
|
| 13 |
+
new_size = int(original_tensor.size(0) * (1 - reduction_factor))
|
| 14 |
+
|
| 15 |
+
# Resize the tensor (this could vary depending on your requirements)
|
| 16 |
+
if new_size > 0: # Ensure new size is positive
|
| 17 |
+
reduced_tensor = original_tensor[:new_size]
|
| 18 |
+
model_data[key] = reduced_tensor
|
| 19 |
+
|
| 20 |
+
# Save the modified model
|
| 21 |
+
save_file(model_data, output_file)
|
| 22 |
+
|
| 23 |
+
# Usage example
|
| 24 |
+
input_file = 'merged_model.safetensors' # Replace with your input model file
|
| 25 |
+
output_file = 'merged_model_16.safetensors' # Desired output file name
|
| 26 |
+
reduce_key_size(input_file, output_file)
|