ljleb / sd-webui-neutral-prompt

Collision-free AND keywords for a1111 webui!
MIT License
187 stars 13 forks source link

Introduce CFGRescaleFactorSingleton class #47

Closed wbclark closed 10 months ago

wbclark commented 11 months ago

This allows other extensions and scripts to get and use the CFG rescale factor: from lib_neutral_prompt.cfg_denoiser_hijack import CFGRescaleFactorSingleton current_rescale_factor = CFGRescaleFactorSingleton.get_cfg_rescale_factor()

My particular use case involves setting self-attention guidance scale per denoising step from the cfg_rescale_factor

ljleb commented 11 months ago

see #48, I implemented it in a way that should be usable by any script no matter whether they run before or after neutral prompt.

wbclark commented 11 months ago

Sorry, I could have explained my use-case better. My goal isn't to export the quantity φ or control it from another extension, although that is a welcome feature for sure (adetailer integration comes to mind).

I want to export the quantity which captures how much the cfg-rescaled guidance differs from the guidance before rescaling and use this quantity within other extensions to dynamically schedule other types of guidance. One application is research into the effects of generalized adaptive guidance mechanisms.

A better implementation might look like a guidance stats module in neutral prompt that makes torch.std(cond) and torch.std(cfg_cond) observable. Would you consider something like this?

ljleb commented 11 months ago

Sure, we can do this. I suggest we replace the torch.std calls with a custom function that takes as input the two conds and the current sampling step number so external code has a way to know where we are, and then implement it as torch.std / torch.std by default if no special function is defined for that step.

Would this solution cover your use case?

ljleb commented 11 months ago

Is there a reason you want neutral prompt to do this btw? It may be better to implement this in another new extension. If this is not possible or may lead to issues for some reason, we can implement it here.

ljleb commented 10 months ago

I'm closing this for now, but please leave a comment if there is still an unmet need that requires a change in the repo and we can find a way to make this work.