Skip to content

Instantly share code, notes, and snippets.

@rvkwi
Last active April 1, 2023 20:27
Show Gist options
  • Save rvkwi/2df007fa3d6249474e18a7aa47d42507 to your computer and use it in GitHub Desktop.
Save rvkwi/2df007fa3d6249474e18a7aa47d42507 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# trims safetensors file of addtional data rewriting only tensors
# CAREFUL, this will remove all metadata as well
# requires safetensors==0.2.7, torch==2.0.0, numpy==1.24.2
import sys
import safetensors
from safetensors.torch import save_file
files = []
try:
for a in sys.argv:
if a.endswith(".safetensors"): files.append(a)
except:
print("no path. quitting")
sys.exit()
for file in files:
print("Processing %s" %(file))
tensors = {}
with safetensors.safe_open(file, framework="pt", device=0) as f:
for k in f.keys(): tensors[k] = f.get_tensor(k)
save_file(tensors, file.replace(".safetensors", "_trim.safetensors"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment