Skip to content

use memory mapping to accelerate hashing of large models. #340

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions py/model_info.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import hashlib
import json
import mmap
from aiohttp import web
from server import PromptServer
import folder_paths
import os


async def generate_sha256(path):
hasher = hashlib.sha256()
with open(path, "rb") as f:
with mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) as mm:
hasher.update(mm)

return hasher.hexdigest()


def get_metadata(filepath):
with open(filepath, "rb") as file:
# https://github.com/huggingface/safetensors#format
Expand All @@ -28,7 +38,7 @@ async def save_notes(request):
name = request.match_info["name"]
pos = name.index("/")
type = name[0:pos]
name = name[pos+1:]
name = name[pos + 1:]

file_path = None
if type == "embeddings" or type == "loras":
Expand Down Expand Up @@ -64,7 +74,7 @@ async def load_metadata(request):
name = request.match_info["name"]
pos = name.index("/")
type = name[0:pos]
name = name[pos+1:]
name = name[pos + 1:]

file_path = None
if type == "embeddings" or type == "loras":
Expand Down Expand Up @@ -107,8 +117,7 @@ async def load_metadata(request):
with open(hash_file, "rt") as f:
meta["pysssss.sha256"] = f.read()
else:
with open(file_path, "rb") as f:
meta["pysssss.sha256"] = hashlib.sha256(f.read()).hexdigest()
meta["pysssss.sha256"] = await generate_sha256(file_path)
with open(hash_file, "wt") as f:
f.write(meta["pysssss.sha256"])

Expand Down