Skip to content
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

Support loading more formats of flux diffusers models #1913

Draft
wants to merge 1 commit into
base: sd3
Choose a base branch
from
Draft
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
36 changes: 32 additions & 4 deletions library/flux_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
MODEL_NAME_SCHNELL = "schnell"


def get_shards(json_path):
try:
with open(json_path, 'r') as f:
data = json.load(f)
weight_map = data.get('weight_map', {})
safetensors_files = list(weight_map.values())
unique_files = list(set(safetensors_files))
return unique_files
except FileNotFoundError:
return []
except json.JSONDecodeError:
print(f"Error: Unable to parse JSON in {json_path}.")
return []


def analyze_checkpoint_state(ckpt_path: str) -> Tuple[bool, bool, Tuple[int, int], List[str]]:
"""
チェックポイントの状態を分析し、DiffusersかBFLか、devかschnellか、ブロック数を計算して返す。
Expand All @@ -42,13 +57,26 @@ def analyze_checkpoint_state(ckpt_path: str) -> Tuple[bool, bool, Tuple[int, int
# check the state dict: Diffusers or BFL, dev or schnell, number of blocks
logger.info(f"Checking the state dict: Diffusers or BFL, dev or schnell")

ckpt_paths = []
if os.path.isdir(ckpt_path): # if ckpt_path is a directory, it is Diffusers
ckpt_path = os.path.join(ckpt_path, "transformer", "diffusion_pytorch_model-00001-of-00003.safetensors")
if "00001-of-00003" in ckpt_path:
ckpt_paths = [ckpt_path.replace("00001-of-00003", f"0000{i}-of-00003") for i in range(1, 4)]
else:
# Check for a transformer directory
transformer_dir = os.path.join(ckpt_path, "transformer")
if os.path.isdir(transformer_dir):
ckpt_path = os.path.join(ckpt_path, "transformer")

# Check for weight_map index file
weight_map_file = os.path.join(ckpt_path, "diffusion_pytorch_model.safetensors.index.json")
if os.path.isfile(weight_map_file):
ckpt_paths = [os.path.join(ckpt_path, file) for file in get_shards(weight_map_file)]
else:
ckpt_path = os.path.join(ckpt_path, "diffusion_pytorch_model-00001-of-00003.safetensors")
ckpt_paths = [ckpt_path.replace("00001-of-00003", f"0000{i}-of-00003") for i in range(1, 4)]
else:
ckpt_paths = [ckpt_path]

if len(ckpt_paths) == 0:
raise RuntimeError("Could not find a checkpoint to analyze / 分析するチェックポイントが見つかりませんでした")

keys = []
for ckpt_path in ckpt_paths:
with safe_open(ckpt_path, framework="pt") as f:
Expand Down