mk / rp.py
pranavajay's picture
Update rp.py
4b10e6f verified
raw
history blame
1.07 kB
import torch
from safetensors.torch import load_file, save_file
def reduce_key_size(input_file, output_file, reduction_factor=0.50):
# Load the model
model_data = load_file(input_file)
# Iterate through all the tensors and reduce their size
for key in model_data.keys():
original_tensor = model_data[key]
# Calculate the new size
new_size = int(original_tensor.size(0) * (1 - reduction_factor))
# Resize the tensor (ensure the new size is positive)
if new_size > 0:
reduced_tensor = original_tensor[:new_size]
# Convert to FP16 precision (half-precision floating point)
fp16_tensor = reduced_tensor.to(torch.float16)
model_data[key] = fp16_tensor
# Save the modified model
save_file(model_data, output_file)
# Usage example
input_file = 'merged_model_216.safetensors' # Replace with your input model file
output_file = 'merged_model_28.safetensors' # Desired output file name
reduce_key_size(input_file, output_file)