-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
better logs and add helper for convert.py
- Loading branch information
1 parent
6cdab7a
commit 1de0dde
Showing
3 changed files
with
168 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,12 @@ | ||
build: | ||
gpu: true | ||
cuda: "11.6" | ||
cuda: "11.8" | ||
python_version: "3.10" | ||
python_packages: | ||
- "diffusers==0.16.1" | ||
# - "opencv-contrib-python==4.6.0.66" | ||
# - "torchvision==0.15.1" | ||
# - "timm==0.6.12" | ||
- "torch==2.0.1" | ||
# - "ftfy==6.1.1" | ||
- "scipy==1.10.1" | ||
- "transformers==4.29.2" | ||
- "accelerate==0.19.0" | ||
run: | ||
# - "pip install git+https://github.com/patrickvonplaten/controlnet_aux.git@dfb604120af6e5acf21936f79aecb046d0faaa29" | ||
- "pip install compel==1.0.4" | ||
- "safetensors==0.3.1" | ||
predict: "predict.py:Predictor" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import torch | ||
import os | ||
from collections import defaultdict | ||
from safetensors.torch import load_file, save_file | ||
|
||
|
||
def check_file_size(sf_filename: str, pt_filename: str): | ||
sf_size = os.stat(sf_filename).st_size | ||
pt_size = os.stat(pt_filename).st_size | ||
|
||
if (sf_size - pt_size) / pt_size > 0.01: | ||
raise RuntimeError( | ||
f"""The file size different is more than 1%: | ||
- {sf_filename}: {sf_size} | ||
- {pt_filename}: {pt_size} | ||
""" | ||
) | ||
|
||
|
||
def shared_pointers(tensors): | ||
ptrs = defaultdict(list) | ||
for k, v in tensors.items(): | ||
ptrs[v.data_ptr()].append(k) | ||
failing = [] | ||
for ptr, names in ptrs.items(): | ||
if len(names) > 1: | ||
failing.append(names) | ||
return failing | ||
|
||
|
||
def convert_file( | ||
pt_filename: str, | ||
sf_filename: str, | ||
): | ||
loaded = torch.load(pt_filename, map_location="cpu") | ||
if "state_dict" in loaded: | ||
loaded = loaded["state_dict"] | ||
shared = shared_pointers(loaded) | ||
for shared_weights in shared: | ||
for name in shared_weights[1:]: | ||
loaded.pop(name) | ||
|
||
# For tensors to be contiguous | ||
loaded = {k: v.contiguous() for k, v in loaded.items()} | ||
|
||
dirname = os.path.dirname(sf_filename) | ||
os.makedirs(dirname, exist_ok=True) | ||
save_file(loaded, sf_filename, metadata={"format": "pt"}) | ||
check_file_size(sf_filename, pt_filename) | ||
reloaded = load_file(sf_filename) | ||
for k in loaded: | ||
pt_tensor = loaded[k] | ||
sf_tensor = reloaded[k] | ||
if not torch.equal(pt_tensor, sf_tensor): | ||
raise RuntimeError(f"The output tensors do not match for key {k}") | ||
|
||
|
||
if __name__ == "__main__": | ||
import glob | ||
for bin_fn in glob.glob('weights/**/*.bin'): | ||
if 'text_encoder' in bin_fn: | ||
continue | ||
st_fn = bin_fn.replace('.bin', '.safetensors') | ||
convert_file(bin_fn, st_fn) | ||
os.remove(bin_fn) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters