hypertile_script.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import hypertile
  2. from modules import scripts, script_callbacks, shared
  3. from scripts.hypertile_xyz import add_axis_options
  4. class ScriptHypertile(scripts.Script):
  5. name = "Hypertile"
  6. def title(self):
  7. return self.name
  8. def show(self, is_img2img):
  9. return scripts.AlwaysVisible
  10. def process(self, p, *args):
  11. hypertile.set_hypertile_seed(p.all_seeds[0])
  12. configure_hypertile(p.width, p.height, enable_unet=shared.opts.hypertile_enable_unet)
  13. self.add_infotext(p)
  14. def before_hr(self, p, *args):
  15. enable = shared.opts.hypertile_enable_unet_secondpass or shared.opts.hypertile_enable_unet
  16. # exclusive hypertile seed for the second pass
  17. if enable:
  18. hypertile.set_hypertile_seed(p.all_seeds[0])
  19. configure_hypertile(p.hr_upscale_to_x, p.hr_upscale_to_y, enable_unet=enable)
  20. if enable and not shared.opts.hypertile_enable_unet:
  21. p.extra_generation_params["Hypertile U-Net second pass"] = True
  22. self.add_infotext(p, add_unet_params=True)
  23. def add_infotext(self, p, add_unet_params=False):
  24. def option(name):
  25. value = getattr(shared.opts, name)
  26. default_value = shared.opts.get_default(name)
  27. return None if value == default_value else value
  28. if shared.opts.hypertile_enable_unet:
  29. p.extra_generation_params["Hypertile U-Net"] = True
  30. if shared.opts.hypertile_enable_unet or add_unet_params:
  31. p.extra_generation_params["Hypertile U-Net max depth"] = option('hypertile_max_depth_unet')
  32. p.extra_generation_params["Hypertile U-Net max tile size"] = option('hypertile_max_tile_unet')
  33. p.extra_generation_params["Hypertile U-Net swap size"] = option('hypertile_swap_size_unet')
  34. if shared.opts.hypertile_enable_vae:
  35. p.extra_generation_params["Hypertile VAE"] = True
  36. p.extra_generation_params["Hypertile VAE max depth"] = option('hypertile_max_depth_vae')
  37. p.extra_generation_params["Hypertile VAE max tile size"] = option('hypertile_max_tile_vae')
  38. p.extra_generation_params["Hypertile VAE swap size"] = option('hypertile_swap_size_vae')
  39. def configure_hypertile(width, height, enable_unet=True):
  40. hypertile.hypertile_hook_model(
  41. shared.sd_model.first_stage_model,
  42. width,
  43. height,
  44. swap_size=shared.opts.hypertile_swap_size_vae,
  45. max_depth=shared.opts.hypertile_max_depth_vae,
  46. tile_size_max=shared.opts.hypertile_max_tile_vae,
  47. enable=shared.opts.hypertile_enable_vae,
  48. )
  49. hypertile.hypertile_hook_model(
  50. shared.sd_model.model,
  51. width,
  52. height,
  53. swap_size=shared.opts.hypertile_swap_size_unet,
  54. max_depth=shared.opts.hypertile_max_depth_unet,
  55. tile_size_max=shared.opts.hypertile_max_tile_unet,
  56. enable=enable_unet,
  57. is_sdxl=shared.sd_model.is_sdxl
  58. )
  59. def on_ui_settings():
  60. import gradio as gr
  61. options = {
  62. "hypertile_explanation": shared.OptionHTML("""
  63. <a href='https://github.com/tfernd/HyperTile'>Hypertile</a> optimizes the self-attention layer within U-Net and VAE models,
  64. resulting in a reduction in computation time ranging from 1 to 4 times. The larger the generated image is, the greater the
  65. benefit.
  66. """),
  67. "hypertile_enable_unet": shared.OptionInfo(False, "Enable Hypertile U-Net", infotext="Hypertile U-Net").info("enables hypertile for all modes, including hires fix second pass; noticeable change in details of the generated picture"),
  68. "hypertile_enable_unet_secondpass": shared.OptionInfo(False, "Enable Hypertile U-Net for hires fix second pass", infotext="Hypertile U-Net second pass").info("enables hypertile just for hires fix second pass - regardless of whether the above setting is enabled"),
  69. "hypertile_max_depth_unet": shared.OptionInfo(3, "Hypertile U-Net max depth", gr.Slider, {"minimum": 0, "maximum": 3, "step": 1}, infotext="Hypertile U-Net max depth").info("larger = more neural network layers affected; minor effect on performance"),
  70. "hypertile_max_tile_unet": shared.OptionInfo(256, "Hypertile U-Net max tile size", gr.Slider, {"minimum": 0, "maximum": 512, "step": 16}, infotext="Hypertile U-Net max tile size").info("larger = worse performance"),
  71. "hypertile_swap_size_unet": shared.OptionInfo(3, "Hypertile U-Net swap size", gr.Slider, {"minimum": 0, "maximum": 64, "step": 1}, infotext="Hypertile U-Net swap size"),
  72. "hypertile_enable_vae": shared.OptionInfo(False, "Enable Hypertile VAE", infotext="Hypertile VAE").info("minimal change in the generated picture"),
  73. "hypertile_max_depth_vae": shared.OptionInfo(3, "Hypertile VAE max depth", gr.Slider, {"minimum": 0, "maximum": 3, "step": 1}, infotext="Hypertile VAE max depth"),
  74. "hypertile_max_tile_vae": shared.OptionInfo(128, "Hypertile VAE max tile size", gr.Slider, {"minimum": 0, "maximum": 512, "step": 16}, infotext="Hypertile VAE max tile size"),
  75. "hypertile_swap_size_vae": shared.OptionInfo(3, "Hypertile VAE swap size ", gr.Slider, {"minimum": 0, "maximum": 64, "step": 1}, infotext="Hypertile VAE swap size"),
  76. }
  77. for name, opt in options.items():
  78. opt.section = ('hypertile', "Hypertile")
  79. shared.opts.add_option(name, opt)
  80. script_callbacks.on_ui_settings(on_ui_settings)
  81. script_callbacks.on_before_ui(add_axis_options)