You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Edit: Oops wrong code for prompt_injection.py before, I've attached one with influence on SVD model below for testing. Especially interested in probe results that found hidden CLIPTextTransformer, CLIPVisionTransformer layers and associated projection layers in the SVD model, but this may be in the documentation? You just have to have it go from svd model to modelmergesdxl node and keep the svd model at 1 for all blocks (messing with some blocks fail but some still help, not required) then use the clip from a checkpoint loader and have it go to clip text encode, then that goes into the attn2 SVD Prompt injection I added, then it goes into the ksampler. I am still testing.
I feel like it's almost sort of working interpolating the clip for injecting svd a little with this repo as base and my code below. Also included the various SVD model probing results, where I put comment dummy data to scan for hidden inputs and outputs in the svd model.
Updated prompt_injection.py with Attn2 Prompt Injection Node: (Please someone help me test!)
import comfy.model_patcher
import comfy.samplers
import torch
import torch.nn.functional as F
def build_patch(patchedBlocks, weight=1.0, sigma_start=0.0, sigma_end=1.0):
def prompt_injection_patch(n, context_attn1: torch.Tensor, value_attn1, extra_options):
(block, block_index) = extra_options.get('block', (None,None))
sigma = extra_options["sigmas"].detach().cpu()[0].item() if 'sigmas' in extra_options else 999999999.9
batch_prompt = n.shape[0] // len(extra_options["cond_or_uncond"])
if sigma <= sigma_start and sigma >= sigma_end:
if (block and f'{block}:{block_index}' in patchedBlocks and patchedBlocks[f'{block}:{block_index}']):
if context_attn1.dim() == 3:
c = context_attn1[0].unsqueeze(0)
else:
c = context_attn1[0][0].unsqueeze(0)
b = patchedBlocks[f'{block}:{block_index}'][0][0].repeat(c.shape[0], 1, 1).to(context_attn1.device)
out = torch.stack((c, b)).to(dtype=context_attn1.dtype) * weight
out = out.repeat(1, batch_prompt, 1, 1) * weight
return n, out, out
return n, context_attn1, value_attn1
return prompt_injection_patch
def build_svd_patch(patchedBlocks, weight=1.0, sigma_start=0.0, sigma_end=1.0):
def prompt_injection_patch(n, context_attn1: torch.Tensor, value_attn1, extra_options):
(block, block_index) = extra_options.get('block', (None, None))
sigma = extra_options["sigmas"].detach().cpu()[0].item() if 'sigmas' in extra_options else 999999999.9
if sigma_start <= sigma <= sigma_end:
if block and f'{block}:{block_index}' in patchedBlocks and patchedBlocks[f'{block}:{block_index}']:
if context_attn1.dim() == 3:
c = context_attn1[0].unsqueeze(0)
else:
c = context_attn1[0][0].unsqueeze(0)
b = patchedBlocks[f'{block}:{block_index}'][0][0].repeat(c.shape[0], 1, 1).to(context_attn1.device)
# Interpolate to match the sizes
if c.size() != b.size():
b = F.interpolate(b.unsqueeze(0), size=c.size()[1:], mode='nearest').squeeze(0)
out = torch.cat((c, b), dim=-1).to(dtype=context_attn1.dtype) * weight
return n, out # Ensure exactly two values are returned for SVD
return n, context_attn1, value_attn1 # Ensure exactly three values are returned
return prompt_injection_patch
class SVDPromptInjection:
@classmethod
def INPUT_TYPES(s):
return {
"required": {"model": ("MODEL",)},
"optional": {
"all": ("CONDITIONING",),
"time_embed": ("CONDITIONING",),
"label_emb": ("CONDITIONING",),
"input_blocks_0": ("CONDITIONING",),
"input_blocks_1": ("CONDITIONING",),
"input_blocks_2": ("CONDITIONING",),
"input_blocks_3": ("CONDITIONING",),
"input_blocks_4": ("CONDITIONING",),
"input_blocks_5": ("CONDITIONING",),
"input_blocks_6": ("CONDITIONING",),
"input_blocks_7": ("CONDITIONING",),
"input_blocks_8": ("CONDITIONING",),
"middle_block_0": ("CONDITIONING",),
"middle_block_1": ("CONDITIONING",),
"middle_block_2": ("CONDITIONING",),
"output_blocks_0": ("CONDITIONING",),
"output_blocks_1": ("CONDITIONING",),
"output_blocks_2": ("CONDITIONING",),
"output_blocks_3": ("CONDITIONING",),
"output_blocks_4": ("CONDITIONING",),
"output_blocks_5": ("CONDITIONING",),
"output_blocks_6": ("CONDITIONING",),
"output_blocks_7": ("CONDITIONING",),
"output_blocks_8": ("CONDITIONING",),
"weight": ("FLOAT", {"default": 1.0, "min": -2.0, "max": 5.0, "step": 0.05}),
"start_at": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
"end_at": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.001}),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "advanced/model"
def patch(self, model: comfy.model_patcher.ModelPatcher, all=None, time_embed=None, label_emb=None, input_blocks_0=None, input_blocks_1=None, input_blocks_2=None, input_blocks_3=None, input_blocks_4=None, input_blocks_5=None, input_blocks_6=None, input_blocks_7=None, input_blocks_8=None, middle_block_0=None, middle_block_1=None, middle_block_2=None, output_blocks_0=None, output_blocks_1=None, output_blocks_2=None, output_blocks_3=None, output_blocks_4=None, output_blocks_5=None, output_blocks_6=None, output_blocks_7=None, output_blocks_8=None, weight=1.0, start_at=0.0, end_at=1.0):
if not any((all, time_embed, label_emb, input_blocks_0, input_blocks_1, input_blocks_2, input_blocks_3, input_blocks_4, input_blocks_5, input_blocks_6, input_blocks_7, input_blocks_8, middle_block_0, middle_block_1, middle_block_2, output_blocks_0, output_blocks_1, output_blocks_2, output_blocks_3, output_blocks_4, output_blocks_5, output_blocks_6, output_blocks_7, output_blocks_8)):
return (model,)
m = model.clone()
sigma_start = m.get_model_object("model_sampling").percent_to_sigma(start_at)
sigma_end = m.get_model_object("model_sampling").percent_to_sigma(end_at)
patchedBlocks = {}
blocks = {
'time_embed': [0],
'label_emb': [0],
'input_blocks': list(range(9)),
'middle_block': list(range(3)),
'output_blocks': list(range(9))
}
for block in blocks:
for index in blocks[block]:
block_name = f"{block}_{index}"
value = locals().get(block_name, None)
if value is None:
value = all
if value is not None:
patchedBlocks[f"{block}:{index}"] = value
m.set_model_attn2_patch(build_svd_patch(patchedBlocks, weight=weight, sigma_start=sigma_start, sigma_end=sigma_end))
return (m,)
class PromptInjection:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
},
"optional": {
"all": ("CONDITIONING",),
"input_4": ("CONDITIONING",),
"input_5": ("CONDITIONING",),
"input_7": ("CONDITIONING",),
"input_8": ("CONDITIONING",),
"middle_0": ("CONDITIONING",),
"output_0": ("CONDITIONING",),
"output_1": ("CONDITIONING",),
"output_2": ("CONDITIONING",),
"output_3": ("CONDITIONING",),
"output_4": ("CONDITIONING",),
"output_5": ("CONDITIONING",),
"weight": ("FLOAT", {"default": 1.0, "min": -2.0, "max": 5.0, "step": 0.05}),
"start_at": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
"end_at": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.001}),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "advanced/model"
def patch(self, model: comfy.model_patcher.ModelPatcher, all=None, input_4=None, input_5=None, input_7=None, input_8=None, middle_0=None, output_0=None, output_1=None, output_2=None, output_3=None, output_4=None, output_5=None, weight=1.0, start_at=0.0, end_at=1.0):
if not any((all, input_4, input_5, input_7, input_8, middle_0, output_0, output_1, output_2, output_3, output_4, output_5)):
return (model,)
m = model.clone()
sigma_start = m.get_model_object("model_sampling").percent_to_sigma(start_at)
sigma_end = m.get_model_object("model_sampling").percent_to_sigma(end_at)
patchedBlocks = {}
blocks = {'input': [4, 5, 7, 8], 'middle': [0], 'output': [0, 1, 2, 3, 4, 5]}
for block in blocks:
for index in blocks[block]:
value = locals()[f"{block}_{index}"] if locals()[f"{block}_{index}"] is not None else all
if value is not None:
patchedBlocks[f"{block}:{index}"] = value
m.set_model_attn2_patch(build_patch(patchedBlocks, weight=weight, sigma_start=sigma_start, sigma_end=sigma_end))
return (m,)
class SimplePromptInjection:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
},
"optional": {
"block": (["input:4", "input:5", "input:7", "input:8", "middle:0", "output:0", "output:1", "output:2", "output:3", "output:4", "output:5"],),
"conditioning": ("CONDITIONING",),
"weight": ("FLOAT", {"default": 1.0, "min": -2.0, "max": 5.0, "step": 0.05}),
"start_at": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
"end_at": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.001}),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "advanced/model"
def patch(self, model: comfy.model_patcher.ModelPatcher, block, conditioning=None, weight=1.0, start_at=0.0, end_at=1.0):
if conditioning is None:
return (model,)
m = model.clone()
sigma_start = m.get_model_object("model_sampling").percent_to_sigma(start_at)
sigma_end = m.get_model_object("model_sampling").percent_to_sigma(end_at)
m.set_model_attn2_patch(build_patch({f"{block}": conditioning}, weight=weight, sigma_start=sigma_start, sigma_end=sigma_end))
return (m,)
class SimplePromptInjection:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
},
"optional": {
"block": (["input:4", "input:5", "input:7", "input:8", "middle:0", "output:0", "output:1", "output:2", "output:3", "output:4", "output:5"],),
"conditioning": ("CONDITIONING",),
"weight": ("FLOAT", {"default": 1.0, "min": -2.0, "max": 5.0, "step": 0.05}),
"start_at": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
"end_at": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.001}),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "advanced/model"
def patch(self, model: comfy.model_patcher.ModelPatcher, block, conditioning=None, weight=1.0, start_at=0.0, end_at=1.0):
if conditioning is None:
return (model,)
m = model.clone()
sigma_start = m.get_model_object("model_sampling").percent_to_sigma(start_at)
sigma_end = m.get_model_object("model_sampling").percent_to_sigma(end_at)
m.set_model_attn2_patch(build_patch({f"{block}": conditioning}, weight=weight, sigma_start=sigma_start, sigma_end=sigma_end))
return (m,)
class AdvancedPromptInjection:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
},
"optional": {
"locations": ("STRING", {"multiline": True, "default": "output:0,1.0\noutput:1,1.0"}),
"conditioning": ("CONDITIONING",),
"start_at": ("FLOAT", {"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.001}),
"end_at": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 1.0, "step": 0.001}),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "advanced/model"
def patch(self, model: comfy.model_patcher.ModelPatcher, locations: str, conditioning=None, start_at=0.0, end_at=1.0):
if not conditioning:
return (model,)
m = model.clone()
sigma_start = m.get_model_object("model_sampling").percent_to_sigma(start_at)
sigma_end = m.get_model_object("model_sampling").percent_to_sigma(end_at)
for line in locations.splitlines():
line = line.strip().strip('\n')
weight = 1.0
if ',' in line:
line, weight = line.split(',')
line = line.strip()
weight = float(weight)
if line:
m.set_model_attn2_patch(build_patch({f"{line}": conditioning}, weight=weight, sigma_start=sigma_start, sigma_end=sigma_end))
return (m,)
NODE_CLASS_MAPPINGS = {
"PromptInjection": PromptInjection,
"SimplePromptInjection": SimplePromptInjection,
"AdvancedPromptInjection": AdvancedPromptInjection,
"SVDPromptInjection": SVDPromptInjection
}
NODE_DISPLAY_NAME_MAPPINGS = {
"PromptInjection": "Attn2 Prompt Injection",
"SimplePromptInjection": "Attn2 Prompt Injection (simple)",
"AdvancedPromptInjection": "Attn2 Prompt Injection (advanced)",
"SVDPromptInjection": "Attn2 SVD Prompt Injection"
}
Here are a bunch of SVD probe results for hidden input/outputs I meantioned above. I was mostly interesting in CLIPTextTransformer, CLIPVisionTransformer:
Common Errors:
NoneType attribute errors: Methods like get_buffer, get_parameter, get_submodule, register_buffer, and register_parameter failed due to encountering NoneType attributes.
Specific argument errors: Methods like register_full_backward_hook and set_extra_state failed due to issues with method-specific arguments.
Internal errors: Methods like load_state_dict, state_dict, and to_empty encountered internal errors related to unexpected keyword arguments or invalid combination of arguments.
Improving the Probing Script
To enhance the probing script further, consider the following updates:
Default Values for Specific Arguments: Ensure that the methods requiring specific argument types are provided with appropriate default values.
Enhanced Error Handling: Add more descriptive error messages and handle specific exceptions gracefully.
Here is an updated function to handle method-specific arguments more effectively:
4th PROBE
From the latest probe execution, we can extract and summarize several key learnings and issues:
Successful Method Probes:
Methods without Arguments:
bfloat16, cpu, cuda, double, eval, float, half, ipu, share_memory, train, type, to_empty, xpu, zero_grad: These methods were successfully executed and returned a WrappedModel instance or NoneType where appropriate.
buffers, children, modules, named_buffers, named_children, named_modules, named_parameters, parameters: These methods returned generator objects successfully, indicating they can iterate over model components.
Partially Successful Method Probes:
Methods with Simple Arguments:
register_backward_hook, register_buffer, register_forward_hook, register_forward_pre_hook, register_full_backward_hook, register_full_backward_pre_hook, register_load_state_dict_post_hook, register_module, register_parameter, register_state_dict_pre_hook: These methods returned RemovableHandle objects or similar, indicating they can accept simple callable arguments.
Error-Inducing Methods:
Methods Requiring Specific Arguments:
add_module, apply, get_buffer, get_extra_state, get_parameter, get_submodule, load_state_dict, register_buffer, set_extra_state, state_dict, to: These methods failed due to missing or inappropriate arguments.
train: This method expected a boolean argument.
forward: Although successfully executed with given inputs, the initial probe indicated a possible mismatch in expected input sizes or missing parameters.
Key Findings from forward Method:
Forward Method Outputs:
Successfully executed with specific input types (e.g., init_image: Tensor, width: int, height: int, video_frames: int, motion_bucket_id: int, fps: int, augmentation_level: float).
Returned a dictionary containing 'positive', 'negative', and 'latent' tensors, confirming the primary functionality and output structure.
Next Steps and Recommendations:
Provide Appropriate Arguments:
For methods like add_module, apply, get_buffer, etc., determine and provide appropriate arguments. Default test values or actual data inputs can be used to handle these methods correctly.
Enhance Probing Script:
Update the probing script to handle methods with specific arguments and provide default values where necessary. Improve error handling to offer more specific messages.
Example update for handling method arguments:
Potential Holes and Additional Inputs:
To fully explore the potential holes in the model and test additional inputs, consider the following:
Additional Input Types:
Based on common practices in video generation models, incorporate more varied input types such as:
noise: A tensor representing Gaussian noise.
motion_vector: A tensor representing motion vectors in the video frames.
style_transfer: A tensor for style transfer operations.
keyframes: A tensor for keyframe extraction and interpolation.
depth_map: A tensor representing depth information of the video frames.
semantic_map: A tensor representing semantic segmentation maps.
Testing Return Types:
Extend the testing to check the return types from various methods and ensure the correct handling of outputs like:
CONDITIONING: Ensure the output is correctly conditioned.
LATENT: Verify the latent space representation is accurate.
FEATURES: Check for additional feature maps or tensors.
Based on the findings from your recent script execution, we have gained a clearer understanding of the model's methods and their input-output types. Here is a summary of the new insights compared to previous findings:
Successful Method Probes:
bfloat16, cpu, cuda, double, eval, float, half, ipu, requires_grad_, share_memory, to, train, xpu, zero_grad:
These methods return a WrappedModel instance, indicating they can be successfully executed without additional parameters. This was a confirmation of their utility in model manipulation and evaluation contexts.
Partially Successful Method Probes:
buffers, children, modules, named_buffers, named_children, named_modules, named_parameters, parameters:
These methods return a generator object, which is useful for iterating over different components of the model.
Error-Inducing Methods:
Several methods like add_module, apply, get_buffer, get_extra_state, get_parameter, get_submodule, load_state_dict, register_*, set_extra_state, type, to_empty:
These methods require additional arguments and cannot be executed with default or empty parameters. They are crucial for more advanced operations but need specific inputs.
Insights from forward Method:
The forward method was successfully executed with specific input types and returned a dictionary containing 'positive', 'negative', and 'latent' tensors. This confirms the primary functionality of your model in terms of its output structure.
Model Summary:
The model summary indicated an output shape of [16, 4, 72, 128] for the WrappedModel, with the total estimated size of the model being 7.08 MB. This suggests that the model is relatively lightweight in terms of memory usage.
Error Handling Improvements:
The script successfully catches and prints errors for methods that require additional parameters, providing a clearer path for debugging and further development.
Next Steps:
Addressing Errors in Probed Methods:
For methods like add_module, apply, get_buffer, etc., you need to provide appropriate parameters when calling these methods. Consider defining these parameters or setting default test values to handle them correctly.
Extending Probing and Summarization:
Extend the probing script to handle more complex scenarios, such as testing with actual data inputs rather than randomly generated tensors. This could provide deeper insights into the model's behavior under real-world conditions.
Further Debugging:
Focus on the methods that failed due to missing arguments. Determine the necessary parameters and retry probing these methods with appropriate inputs.
Based on your latest probe execution, we have gathered the following insights:
Successful Method Probes:
bfloat16, cpu, cuda, double, eval, float, half, ipu, requires_grad_, share_memory, to_empty, train, type, xpu, zero_grad:
These methods return a WrappedModel instance, indicating they can be successfully executed without additional parameters.
Partially Successful Method Probes:
buffers, children, modules, named_buffers, named_children, named_modules, named_parameters, parameters:
These methods return a generator object, which is useful for iterating over different components of the model.
Error-Inducing Methods:
Methods like add_module, apply, get_buffer, get_extra_state, get_parameter, get_submodule, load_state_dict, register_*, set_extra_state, state_dict, to:
These methods require additional arguments and cannot be executed with default or empty parameters.
Insights from forward Method:
The forward method was successfully executed with specific input types and returned a dictionary containing 'positive', 'negative', and 'latent' tensors. This confirms the primary functionality of your model in terms of its output structure.
Error Handling Improvements:
The script successfully catches and prints errors for methods that require additional parameters, providing a clearer path for debugging and further development.
Next Steps:
Addressing Errors in Probed Methods:
For methods like add_module, apply, get_buffer, etc., you need to provide appropriate parameters when calling these methods. Consider defining these parameters or setting default test values to handle them correctly.
Extending Probing and Summarization:
Extend the probing script to handle more complex scenarios, such as testing with actual data inputs rather than randomly generated tensors. This could provide deeper insights into the model's behavior under real-world conditions.
Further Debugging:
Focus on the methods that failed due to missing arguments. Determine the necessary parameters and retry probing these methods with appropriate inputs.
Update the Probing Script:
Enhance the script to provide more specific error messages and handle various input types. Here’s an updated snippet for the probing script:
The text was updated successfully, but these errors were encountered:
311-code
changed the title
Could this code I'm working on be a good starting point for modifying SVD blocks? (clip interpolation)
Made a version to influence SVD (please help me test)
Jun 9, 2024
Edit: Oops wrong code for prompt_injection.py before, I've attached one with influence on SVD model below for testing. Especially interested in probe results that found hidden CLIPTextTransformer, CLIPVisionTransformer layers and associated projection layers in the SVD model, but this may be in the documentation? You just have to have it go from svd model to modelmergesdxl node and keep the svd model at 1 for all blocks (messing with some blocks fail but some still help, not required) then use the clip from a checkpoint loader and have it go to clip text encode, then that goes into the attn2 SVD Prompt injection I added, then it goes into the ksampler. I am still testing.
I feel like it's almost sort of working interpolating the clip for injecting svd a little with this repo as base and my code below. Also included the various SVD model probing results, where I put comment dummy data to scan for hidden inputs and outputs in the svd model.
Updated prompt_injection.py with Attn2 Prompt Injection Node: (Please someone help me test!)
Here are a bunch of SVD probe results for hidden input/outputs I meantioned above. I was mostly interesting in CLIPTextTransformer, CLIPVisionTransformer:
The text was updated successfully, but these errors were encountered: