Skip to content

Commit 3f200a7

Browse files
w-e-wnpc-riddlah
andcommitted
cache partial hash
aka old hash Co-Authored-By: npc-riddlah <[email protected]>
1 parent dce4a4c commit 3f200a7

File tree

2 files changed

+31
-17
lines changed

2 files changed

+31
-17
lines changed

modules/hashes.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import hashlib
22
import os.path
33

4-
from modules import shared
4+
from modules import shared, errors
55
import modules.cache
66

77
dump_cache = modules.cache.dump_cache
@@ -82,3 +82,31 @@ def addnet_hash_safetensors(b):
8282

8383
return hash_sha256.hexdigest()
8484

85+
86+
def partial_hash_from_cache(filename, ignore_cache=False):
87+
"""old hash that only looks at a small part of the file and is prone to collisions
88+
kept for compatibility, don't use this for new things
89+
"""
90+
try:
91+
filename = str(filename)
92+
mtime = os.path.getmtime(filename)
93+
hashes = cache('partial-hash')
94+
cache_entry = hashes.get(filename, {})
95+
cache_mtime = cache_entry.get("mtime", 0)
96+
cache_hash = cache_entry.get("hash", None)
97+
if mtime == cache_mtime and cache_hash and not ignore_cache:
98+
return cache_hash
99+
100+
with open(filename, 'rb') as file:
101+
m = hashlib.sha256()
102+
file.seek(0x100000)
103+
m.update(file.read(0x10000))
104+
partial_hash = m.hexdigest()
105+
hashes[filename] = {'mtime': mtime, 'hash': partial_hash}
106+
return partial_hash[0:8]
107+
108+
except FileNotFoundError:
109+
pass
110+
except Exception:
111+
errors.report(f'Error calculating partial hash for {filename}', exc_info=True)
112+
return 'NOFILE'

modules/sd_models.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ldm.modules.midas as midas
1414

1515
from modules import paths, shared, modelloader, devices, script_callbacks, sd_vae, sd_disable_initialization, errors, hashes, sd_models_config, sd_unet, sd_models_xl, cache, extra_networks, processing, lowvram, sd_hijack, patches
16+
from modules.hashes import partial_hash_from_cache as model_hash # noqa: backwards compatibility
1617
from modules.timer import Timer
1718
from modules.shared import opts
1819
import tomesd
@@ -87,7 +88,7 @@ def read_metadata():
8788
self.name = name
8889
self.name_for_extra = os.path.splitext(os.path.basename(filename))[0]
8990
self.model_name = os.path.splitext(name.replace("/", "_").replace("\\", "_"))[0]
90-
self.hash = model_hash(filename)
91+
self.hash = hashes.partial_hash_from_cache(filename)
9192

9293
self.sha256 = hashes.sha256_from_cache(self.filename, f"checkpoint/{name}")
9394
self.shorthash = self.sha256[0:10] if self.sha256 else None
@@ -200,21 +201,6 @@ def get_closet_checkpoint_match(search_string):
200201
return None
201202

202203

203-
def model_hash(filename):
204-
"""old hash that only looks at a small part of the file and is prone to collisions"""
205-
206-
try:
207-
with open(filename, "rb") as file:
208-
import hashlib
209-
m = hashlib.sha256()
210-
211-
file.seek(0x100000)
212-
m.update(file.read(0x10000))
213-
return m.hexdigest()[0:8]
214-
except FileNotFoundError:
215-
return 'NOFILE'
216-
217-
218204
def select_checkpoint():
219205
"""Raises `FileNotFoundError` if no checkpoints are found."""
220206
model_checkpoint = shared.opts.sd_model_checkpoint

0 commit comments

Comments
 (0)