From 0e165ed2ee45238133d0132c018280c97eb0f053 Mon Sep 17 00:00:00 2001 From: nekoworkshop Date: Sat, 29 Apr 2023 17:18:01 -0400 Subject: [PATCH] Use SharedSettingsStackHelper --- scripts/xyz_grid.py | 54 ++++++++++++++++++++++++++++++++------------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/scripts/xyz_grid.py b/scripts/xyz_grid.py index 68c294959..2d785bf04 100644 --- a/scripts/xyz_grid.py +++ b/scripts/xyz_grid.py @@ -145,39 +145,40 @@ def apply_face_restore(p, opt, x): p.restore_faces = is_active -def apply_token_merging1(p, x, xs): - is_active = x.lower() in ('true', 'yes', 'y', '1') - p.override_settings["token_merging"] = is_active - def apply_token_merging_ratio_hr(p, x, xs): - p.override_settings["token_merging_ratio_hr"] = x + opts.data["token_merging_ratio_hr"] = x def apply_token_merging_ratio(p, x, xs): - p.override_settings["token_merging_ratio"] = x + opts.data["token_merging_ratio"] = x def apply_token_merging_hr_only(p, x, xs): is_active = x.lower() in ('true', 'yes', 'y', '1') - p.override_settings["token_merging_hr_only"] = is_active + opts.data["token_merging_hr_only"] = is_active def apply_token_merging_random(p, x, xs): is_active = x.lower() in ('true', 'yes', 'y', '1') - p.override_settings["token_merging_random"] = is_active + opts.data["token_merging_random"] = is_active def apply_token_merging_attention(p, x, xs): is_active = x.lower() in ('true', 'yes', 'y', '1') - p.override_settings["token_merging_merge_attention"] = is_active + opts.data["token_merging_merge_attention"] = is_active def apply_token_merging_cross_attention(p, x, xs): is_active = x.lower() in ('true', 'yes', 'y', '1') - p.override_settings["token_merging_merge_cross_attention"] = is_active + opts.data["token_merging_merge_cross_attention"] = is_active def apply_token_merging_mlp(p, x, xs): is_active = x.lower() in ('true', 'yes', 'y', '1') - p.override_settings["token_merging_merge_mlp"] = is_active + opts.data["token_merging_merge_mlp"] = is_active def apply_token_merging_maximum_down_sampling (p, x, xs): - p.override_settings["token_merging_maximum_down_sampling"] = x - #opts.data["token_merging_maximum_down_sampling"] = x + opts.data["token_merging_maximum_down_sampling"] = x + +def apply_token_merging_stride_x(p, x, xs): + opts.data["token_merging_stride_x"] = x + +def apply_token_merging_stride_y(p, x, xs): + opts.data["token_merging_stride_y"] = x def format_value_add_label(p, opt, x): if type(x) == float: @@ -259,7 +260,6 @@ axis_options = [ AxisOption("Styles", str, apply_styles, choices=lambda: list(shared.prompt_styles.styles)), AxisOption("UniPC Order", int, apply_uni_pc_order, cost=0.5), AxisOption("Face restore", str, apply_face_restore, format_value=format_value), - AxisOption("Token Merging", str, apply_token_merging1), AxisOption("Token merging ratio",float,apply_token_merging_ratio), AxisOption("Token merging ratio for Hires fix",float,apply_token_merging_ratio_hr), AxisOption("Token merging apply only to Hires fix",str,apply_token_merging_hr_only, choices= lambda: ["Yes","No"]), @@ -267,7 +267,9 @@ axis_options = [ AxisOption("Token Merging merge attention", str, apply_token_merging_attention, choices= lambda: ["Yes","No"]), AxisOption("Token Merging merge cross attention", str, apply_token_merging_cross_attention, choices= lambda: ["Yes","No"]), AxisOption("Token Merging merge mlp", str, apply_token_merging_mlp, choices= lambda: ["Yes","No"]), - AxisOption("Token Merging maxium down sampling", int, apply_token_merging_maximum_down_sampling, choices= lambda: ["1","2","4","8"]) + AxisOption("Token Merging maxium down sampling", int, apply_token_merging_maximum_down_sampling, choices= lambda: ["1","2","4","8"]), + AxisOption("Token Merging Stride - X", int, apply_token_merging_stride_x, choices= lambda: ["2","4","6","8"]), + AxisOption("Token Merging Stride - Y", int, apply_token_merging_stride_y, choices= lambda: ["2","4","6","8"]) ] @@ -384,11 +386,23 @@ def draw_xyz_grid(p, xs, ys, zs, x_labels, y_labels, z_labels, cell, draw_legend class SharedSettingsStackHelper(object): def __enter__(self): + #Save overridden settings so they can be restored later. self.CLIP_stop_at_last_layers = opts.CLIP_stop_at_last_layers self.vae = opts.sd_vae self.uni_pc_order = opts.uni_pc_order + self.token_merging_ratio_hr = opts.token_merging_ratio_hr + self.token_merging_ratio = opts.token_merging_ratio + self.token_merging_hr_only = opts.token_merging_hr_only + self.token_merging_random = opts.token_merging_random + self.token_merging_merge_attention = opts.token_merging_merge_attention + self.token_merging_merge_cross_attention = opts.token_merging_merge_cross_attention + self.token_merging_merge_mlp = opts.token_merging_merge_mlp + self.token_merging_maximum_down_sampling = opts.token_merging_maximum_down_sampling + self.token_merging_stride_x = opts.token_merging_stride_x + self.token_merging_stride_y = opts.token_merging_stride_y def __exit__(self, exc_type, exc_value, tb): + #Restore overriden settings after plot generation. opts.data["sd_vae"] = self.vae opts.data["uni_pc_order"] = self.uni_pc_order sd_models.reload_model_weights() @@ -396,6 +410,16 @@ class SharedSettingsStackHelper(object): opts.data["CLIP_stop_at_last_layers"] = self.CLIP_stop_at_last_layers + opts.data["token_merging_ratio_hr"] = self.token_merging_ratio_hr + opts.data["token_merging_ratio"] = self.token_merging_ratio + opts.data["token_merging_hr_only"] = self.token_merging_hr_only + opts.data["token_merging_random"] = self.token_merging_random + opts.data["token_merging_merge_attention"] = self.token_merging_merge_attention + opts.data["token_merging_merge_cross_attention"] = self.token_merging_merge_cross_attention + opts.data["token_merging_merge_mlp"] = self.token_merging_merge_mlp + opts.data["token_merging_maximum_down_sampling"] = self.token_merging_maximum_down_sampling + opts.data["token_merging_stride_x"] = self.token_merging_stride_x + opts.data["token_merging_stride_y"] = self.token_merging_stride_y re_range = re.compile(r"\s*([+-]?\s*\d+)\s*-\s*([+-]?\s*\d+)(?:\s*\(([+-]\d+)\s*\))?\s*") re_range_float = re.compile(r"\s*([+-]?\s*\d+(?:.\d*)?)\s*-\s*([+-]?\s*\d+(?:.\d*)?)(?:\s*\(([+-]\d+(?:.\d*)?)\s*\))?\s*")