Implementation:AUTOMATIC1111 Stable diffusion webui File Hashing
| Knowledge Sources | |
|---|---|
| Domains | Hashing, Model_Management |
| Last Updated | 2025-05-15 00:00 GMT |
Overview
Provides SHA-256 hashing utilities for model files with disk-based caching and support for the kohya-ss addnet safetensors hash format.
Description
The File Hashing module computes SHA-256 hashes of model files and caches the results using the disk cache system. The calculate_sha256 function reads a file in 1 MB chunks and returns the full hex digest. The sha256 function wraps this with cache-aware logic: it first checks the cache for a previously computed hash, comparing the file's current modification time against the cached modification time to detect changes. If the cache is stale or missing, it computes a fresh hash and stores it. The module also supports the kohya-ss addnet hash format for safetensors files via addnet_hash_safetensors, which skips the safetensors header (determined by the first 8 bytes as a little-endian length) and hashes only the tensor data. Hashing can be disabled globally via the --no-hashing command-line flag.
Usage
Use this module to compute and retrieve SHA-256 hashes of model files for identification, integrity verification, and compatibility checking with model registries such as Civitai.
Code Reference
Source Location
- Repository: AUTOMATIC1111_Stable_diffusion_webui
- File: modules/hashes.py
- Lines: 1-84
Signature
def calculate_sha256(filename: str) -> str
def sha256_from_cache(filename: str, title: str, use_addnet_hash: bool = False) -> str | None
def sha256(filename: str, title: str, use_addnet_hash: bool = False) -> str | None
def addnet_hash_safetensors(b: BinaryIO) -> str
Import
from modules.hashes import sha256, calculate_sha256
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| filename | str | Yes | Path to the file to hash |
| title | str | Yes | Cache key identifying this file entry |
| use_addnet_hash | bool | No | When True, uses the kohya-ss addnet safetensors hashing method that skips the header |
| b | BinaryIO | Yes | A binary file-like object for addnet_hash_safetensors |
Outputs
| Name | Type | Description |
|---|---|---|
| hash | str or None | The SHA-256 hex digest of the file, or None if hashing is disabled or the file is not found |
Usage Examples
from modules.hashes import sha256, calculate_sha256
# Compute hash with caching
model_hash = sha256("/models/v1-5-pruned.safetensors", "v1-5-pruned")
print(f"Model hash: {model_hash}")
# Compute hash without caching
raw_hash = calculate_sha256("/models/v1-5-pruned.safetensors")
# Check cached hash only (no computation)
from modules.hashes import sha256_from_cache
cached = sha256_from_cache("/models/v1-5-pruned.safetensors", "v1-5-pruned")