Skip to content
Python

added configurable prompt enhancer size

DeepBeepMeep5b995e703d74
diff --git a/docs/FINETUNES.md b/docs/FINETUNES.md
index b9cf0cf..ac3bef3 100644
--- a/docs/FINETUNES.md
+++ b/docs/FINETUNES.md
@@ -72,6 +72,8 @@ be able to reduce the number of generation steps to 10
-*image_outputs* : turn any model that generates a video into a model that generates images. In fact it will adapt the user interface for image generation and ask the model to generate a video with a single frame.
-*video_prompt_enhancer_instructions* : this allows you override the system prompt used by the Prompt Enhancer when generating a Video with this finetune
-*image_prompt_enhancer_instructions* : this allows you override the system prompt used by the Prompt Enhancer when generating an Image with this finetune
+-*video_prompt_enhancer_max_tokens*: override for the maximum number of tokens generated by the prompt enhancer when generating a Video (default 256)
+-*image_prompt_enhancer_max_tokens*: override for the maximum number of tokens generated by the prompt enhancer when generating an Image (default 256)
In order to favor reusability the properties of *URLs*, *modules*, *loras* and *preload_URLs* can contain instead of a list of URLs a single text which corresponds to the id of a finetune or default model to reuse. Instead of:
```
diff --git a/models/z_image/transformer_z_image.py b/models/z_image/transformer_z_image.py
index 7c46ecf..7e242a8 100644
--- a/models/z_image/transformer_z_image.py
+++ b/models/z_image/transformer_z_image.py
@@ -168,6 +168,18 @@ class FeedForward(nn.Module):
def forward(self, x):
return self.w2(self._forward_silu_gating(self.w1(x), self.w3(x)))
+ def forward_into(self, x, out):
+ assert out.shape == x.shape
+ x1 = self.w1(x) # (..., hidden_dim)
+ x3 = self.w3(x) # (..., hidden_dim)
+ F.silu(x1, inplace=True) # x1 = silu(x1)
+ x1.mul_(x3) # x1 = x1 * x3
+ torch.matmul(
+ x1.view(-1, self.hidden_dim),
+ self.w2.weight.t(),
+ out=out.view(-1, self.dim),
+ )
+ return out
@maybe_allow_in_graph
class ZImageTransformerBlock(nn.Module):
@@ -198,8 +210,8 @@ class ZImageTransformerBlock(nn.Module):
out_bias=False,
processor=ZSingleStreamAttnProcessor(),
)
-
- self.feed_forward = FeedForward(dim=dim, hidden_dim=int(dim / 3 * 8))
+ self.ffn_mult = 8 / 3
+ self.feed_forward = FeedForward(dim=dim, hidden_dim=int(dim * self.ffn_mult))
self.layer_id = layer_id
self.attention_norm1 = RMSNorm(dim, eps=norm_eps)
@@ -220,9 +232,10 @@ class ZImageTransformerBlock(nn.Module):
if scale is not None:
ffn_in.mul_(scale)
ffn_in = ffn_in.reshape(-1, dim)
- chunk_size = max(int(seq_len // 6), 1)
+ chunk_size = max(int(seq_len // self.ffn_mult), 1)
for ffn_chunk in torch.split(ffn_in, chunk_size):
- ffn_chunk.copy_(self.feed_forward(ffn_chunk))
+ ffn_chunk[...]= self.feed_forward(ffn_chunk)
+
return self.ffn_norm2(ffn_in.view(batch, seq_len, dim))
def forward(
diff --git a/wgp.py b/wgp.py
index 809ff68..e9f8fa5 100644
--- a/wgp.py
+++ b/wgp.py
@@ -4734,9 +4734,11 @@ class DynamicClass:
return self.assign(**dict)
-def process_prompt_enhancer(prompt_enhancer, original_prompts, image_start, original_image_refs, is_image, audio_only, seed, prompt_enhancer_instructions = None ):
+def process_prompt_enhancer(model_def, prompt_enhancer, original_prompts, image_start, original_image_refs, is_image, audio_only, seed, prompt_enhancer_instructions = None ):
+
+ prompt_enhancer_instructions = model_def.get("image_prompt_enhancer_instructions" if is_image else "video_prompt_enhancer_instructions", None)
+ text_encoder_max_tokens = model_def.get("image_prompt_enhancer_max_tokens" if is_image else "video_prompt_enhancer_max_tokens", 256)
- text_encoder_max_tokens = 256
from models.ltx_video.utils.prompt_enhance_utils import generate_cinematic_prompt
prompt_images = []
if "I" in prompt_enhancer:
@@ -4821,9 +4823,9 @@ def enhance_prompt(state, prompt, prompt_enhancer, multi_images_gen_type, overri
acquire_GPU_ressources(state, "prompt_enhancer", "Prompt Enhancer")
if enhancer_offloadobj is None:
- profile, _ = init_pipe(pipe, kwargs, override_profile)
+ _, mmgp_profile = init_pipe(pipe, kwargs, override_profile)
setup_prompt_enhancer(pipe, kwargs)
- enhancer_offloadobj = offload.profile(pipe, profile_no= int(profile), **kwargs)
+ enhancer_offloadobj = offload.profile(pipe, profile_no= mmgp_profile, **kwargs)
original_image_refs = inputs["image_refs"]
if original_image_refs is not None:
@@ -4832,7 +4834,6 @@ def enhance_prompt(state, prompt, prompt_enhancer, multi_images_gen_type, overri
seed = inputs["seed"]
seed = set_seed(seed)
enhanced_prompts = []
- prompt_enhancer_instructions = model_def.get("image_prompt_enhancer_instructions" if is_image else "video_prompt_enhancer_instructions", None)
for i, (one_prompt, one_image) in enumerate(zip(original_prompts, image_start)):
start_images = [one_image] if one_image is not None else None
@@ -4840,7 +4841,7 @@ def enhance_prompt(state, prompt, prompt_enhancer, multi_images_gen_type, overri
progress((i , num_prompts), desc=status, total= num_prompts)
try:
- enhanced_prompt = process_prompt_enhancer(prompt_enhancer, [one_prompt], start_images, original_image_refs, is_image, audio_only, seed, prompt_enhancer_instructions )
+ enhanced_prompt = process_prompt_enhancer(model_def, prompt_enhancer, [one_prompt], start_images, original_image_refs, is_image, audio_only, seed)
except Exception as e:
enhancer_offloadobj.unload_all()
release_GPU_ressources(state, "prompt_enhancer")
@@ -5332,7 +5333,7 @@ def generate_video(
start_time = time.time()
if prompt_enhancer_image_caption_model != None and prompt_enhancer !=None and len(prompt_enhancer)>0 and server_config.get("enhancer_mode", 0) == 0:
send_cmd("progress", [0, get_latest_status(state, "Enhancing Prompt")])
- enhanced_prompts = process_prompt_enhancer(prompt_enhancer, original_prompts, image_start, original_image_refs, is_image, audio_only, seed )
+ enhanced_prompts = process_prompt_enhancer(model_def, prompt_enhancer, original_prompts, image_start, original_image_refs, is_image, audio_only, seed )
if enhanced_prompts is not None:
print(f"Enhanced prompts: {enhanced_prompts}" )
task["prompt"] = "\n".join(["!enhanced!"] + enhanced_prompts)