Fix: Prevent TypeError: 'NoneType' is not iterable on model switching (Forge/reForge)

This fixes the TypeError that occurs during model switching in sd-webui-reforge and possibly other optimized environments.

The error happens because 'get_token_counter' tries to access 'sd_models.model_data.sd_model.cond_stage_model' before the model is fully loaded (when 'sd_model' is None).

The fix adds a check for the 'None' value to safely return 0 tokens instead of throwing an error.
This commit is contained in:
ねおん
2025-11-04 03:14:14 +09:00
committed by GitHub
parent b00149b4c7
commit 9e1ced6438

View File

@@ -27,6 +27,10 @@ def get_token_counter(text, steps):
prompts = [prompt_text for step, prompt_text in flat_prompts]
if forge:
# Check if the model is fully loaded to prevent TypeError during model switching
if sd_models.model_data.sd_model is None:
return {"token_count": 0, "max_length": 0}
cond_stage_model = sd_models.model_data.sd_model.cond_stage_model
token_count, max_length = max([model_hijack.get_prompt_lengths(prompt,cond_stage_model) for prompt in prompts],
key=lambda args: args[0])