2023-06-13 19:34:35 -04:00
|
|
|
import functools
|
2024-02-06 11:51:34 -05:00
|
|
|
import pprint
|
2023-11-18 16:31:41 -05:00
|
|
|
import random
|
2023-06-13 19:34:35 -04:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
2023-11-18 16:31:41 -05:00
|
|
|
from modules import shared
|
|
|
|
from modules.loaders import loaders_samplers
|
2024-01-08 21:28:35 -05:00
|
|
|
from modules.logging_colors import logger
|
2023-11-18 16:31:41 -05:00
|
|
|
|
2023-06-13 19:34:35 -04:00
|
|
|
|
2023-07-31 22:13:29 -04:00
|
|
|
def default_preset():
|
|
|
|
return {
|
2023-06-13 19:34:35 -04:00
|
|
|
'temperature': 1,
|
2023-11-04 12:09:07 -04:00
|
|
|
'temperature_last': False,
|
2024-01-07 15:03:47 -05:00
|
|
|
'dynamic_temperature': False,
|
2024-01-08 21:28:35 -05:00
|
|
|
'dynatemp_low': 1,
|
|
|
|
'dynatemp_high': 1,
|
|
|
|
'dynatemp_exponent': 1,
|
2024-02-03 22:20:02 -05:00
|
|
|
'smoothing_factor': 0,
|
2024-03-03 11:22:21 -05:00
|
|
|
'smoothing_curve': 1,
|
2023-06-13 19:34:35 -04:00
|
|
|
'top_p': 1,
|
2023-11-03 11:25:22 -04:00
|
|
|
'min_p': 0,
|
2023-08-06 16:22:48 -04:00
|
|
|
'top_k': 0,
|
2023-06-13 19:34:35 -04:00
|
|
|
'repetition_penalty': 1,
|
2023-10-25 11:10:28 -04:00
|
|
|
'presence_penalty': 0,
|
|
|
|
'frequency_penalty': 0,
|
2023-12-07 18:04:52 -05:00
|
|
|
'repetition_penalty_range': 1024,
|
2023-11-06 00:38:29 -05:00
|
|
|
'typical_p': 1,
|
|
|
|
'tfs': 1,
|
|
|
|
'top_a': 0,
|
|
|
|
'epsilon_cutoff': 0,
|
|
|
|
'eta_cutoff': 0,
|
2023-08-06 16:22:48 -04:00
|
|
|
'guidance_scale': 1,
|
2023-11-06 00:38:29 -05:00
|
|
|
'penalty_alpha': 0,
|
2023-06-13 19:34:35 -04:00
|
|
|
'mirostat_mode': 0,
|
2023-11-06 00:38:29 -05:00
|
|
|
'mirostat_tau': 5,
|
2023-06-13 19:34:35 -04:00
|
|
|
'mirostat_eta': 0.1,
|
2023-11-06 00:38:29 -05:00
|
|
|
'do_sample': True,
|
|
|
|
'encoder_repetition_penalty': 1,
|
|
|
|
'no_repeat_ngram_size': 0,
|
|
|
|
'min_length': 0,
|
2023-08-06 16:22:48 -04:00
|
|
|
'num_beams': 1,
|
|
|
|
'length_penalty': 1,
|
|
|
|
'early_stopping': False,
|
2024-02-06 09:20:10 -05:00
|
|
|
'sampler_priority': 'temperature\ndynamic_temperature\nquadratic_sampling\ntop_k\ntop_p\ntypical_p\nepsilon_cutoff\neta_cutoff\ntfs\ntop_a\nmin_p\nmirostat'
|
2023-06-13 19:34:35 -04:00
|
|
|
}
|
|
|
|
|
2023-07-31 22:13:29 -04:00
|
|
|
|
2023-08-06 16:22:48 -04:00
|
|
|
def presets_params():
|
|
|
|
return [k for k in default_preset()]
|
|
|
|
|
|
|
|
|
2023-07-31 22:13:29 -04:00
|
|
|
def load_preset(name):
|
|
|
|
generate_params = default_preset()
|
2023-07-03 23:03:30 -04:00
|
|
|
if name not in ['None', None, '']:
|
2024-01-08 21:28:35 -05:00
|
|
|
path = Path(f'presets/{name}.yaml')
|
|
|
|
if path.exists():
|
|
|
|
with open(path, 'r') as infile:
|
|
|
|
preset = yaml.safe_load(infile)
|
|
|
|
|
|
|
|
for k in preset:
|
|
|
|
generate_params[k] = preset[k]
|
|
|
|
else:
|
|
|
|
logger.error(f"The preset \"{name}\" does not exist under \"{path}\". Using the default parameters.")
|
2023-06-13 19:34:35 -04:00
|
|
|
|
|
|
|
return generate_params
|
|
|
|
|
|
|
|
|
|
|
|
@functools.cache
|
|
|
|
def load_preset_memoized(name):
|
|
|
|
return load_preset(name)
|
|
|
|
|
|
|
|
|
|
|
|
def load_preset_for_ui(name, state):
|
|
|
|
generate_params = load_preset(name)
|
|
|
|
state.update(generate_params)
|
2023-08-06 16:22:48 -04:00
|
|
|
return state, *[generate_params[k] for k in presets_params()]
|
2023-06-13 19:34:35 -04:00
|
|
|
|
|
|
|
|
2023-11-18 16:31:41 -05:00
|
|
|
def random_preset(state):
|
|
|
|
params_and_values = {
|
|
|
|
'remove_tail_tokens': {
|
|
|
|
'top_p': [0.5, 0.8, 0.9, 0.95, 0.99],
|
|
|
|
'min_p': [0.5, 0.2, 0.1, 0.05, 0.01],
|
|
|
|
'top_k': [3, 5, 10, 20, 30, 40],
|
|
|
|
'typical_p': [0.2, 0.575, 0.95],
|
|
|
|
'tfs': [0.5, 0.8, 0.9, 0.95, 0.99],
|
|
|
|
'top_a': [0.5, 0.2, 0.1, 0.05, 0.01],
|
|
|
|
'epsilon_cutoff': [1, 3, 5, 7, 9],
|
|
|
|
'eta_cutoff': [3, 6, 9, 12, 15, 18],
|
|
|
|
},
|
|
|
|
'flatten_distribution': {
|
2024-02-06 11:51:34 -05:00
|
|
|
'temperature': [0.1, 0.5, 0.7, 0.8, 1, 1.2, 1.5, 2.0, 5.0],
|
|
|
|
'dynamic_temperature': [
|
|
|
|
[0.1, 1],
|
|
|
|
[0.1, 1.5],
|
|
|
|
[0.1, 2],
|
|
|
|
[0.1, 5],
|
|
|
|
[0.5, 1],
|
|
|
|
[0.5, 1.5],
|
|
|
|
[0.5, 2],
|
|
|
|
[0.5, 5],
|
|
|
|
[0.8, 1],
|
|
|
|
[0.8, 1.5],
|
|
|
|
[0.8, 2],
|
|
|
|
[0.8, 5],
|
|
|
|
[1, 1.5],
|
|
|
|
[1, 2],
|
|
|
|
[1, 5]
|
|
|
|
],
|
2024-03-03 11:22:21 -05:00
|
|
|
'smoothing_factor': [0.2, 0.3, 0.6, 1.2],
|
2023-11-18 16:31:41 -05:00
|
|
|
},
|
|
|
|
'repetition': {
|
|
|
|
'repetition_penalty': [1, 1.05, 1.1, 1.15, 1.20, 1.25],
|
|
|
|
'presence_penalty': [0, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 2.0],
|
|
|
|
'frequency_penalty': [0, 0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 2.0],
|
|
|
|
},
|
|
|
|
'other': {
|
|
|
|
'temperature_last': [True, False],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
generate_params = default_preset()
|
|
|
|
for cat in params_and_values:
|
|
|
|
choices = list(params_and_values[cat].keys())
|
|
|
|
if shared.args.loader is not None:
|
2024-02-13 19:05:41 -05:00
|
|
|
choices = [x for x in choices if loader_contains(x)]
|
2023-11-18 16:31:41 -05:00
|
|
|
|
|
|
|
if len(choices) > 0:
|
2024-01-08 19:46:24 -05:00
|
|
|
choice = random.choice(choices)
|
2024-02-06 11:51:34 -05:00
|
|
|
value = random.choice(params_and_values[cat][choice])
|
|
|
|
if choice == 'dynamic_temperature':
|
|
|
|
generate_params['dynamic_temperature'] = True
|
|
|
|
generate_params['dynatemp_low'] = value[0]
|
|
|
|
generate_params['dynatemp_high'] = value[1]
|
|
|
|
else:
|
|
|
|
generate_params[choice] = value
|
2023-11-18 16:31:41 -05:00
|
|
|
|
|
|
|
state.update(generate_params)
|
2024-02-06 11:51:34 -05:00
|
|
|
logger.info("GENERATED_PRESET=")
|
|
|
|
pprint.PrettyPrinter(indent=4, width=1, sort_dicts=False).pprint(remove_defaults(state))
|
2023-11-18 16:31:41 -05:00
|
|
|
return state, *[generate_params[k] for k in presets_params()]
|
|
|
|
|
|
|
|
|
2024-02-06 11:51:34 -05:00
|
|
|
def loader_contains(sampler):
|
|
|
|
if sampler == 'dynamic_temperature' and 'dynatemp_low' in loaders_samplers[shared.args.loader]:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return sampler in loaders_samplers[shared.args.loader]
|
|
|
|
|
|
|
|
|
|
|
|
def remove_defaults(state):
|
2023-07-31 22:13:29 -04:00
|
|
|
defaults = default_preset()
|
2023-08-06 16:22:48 -04:00
|
|
|
data = {k: state[k] for k in presets_params()}
|
2023-07-31 22:13:29 -04:00
|
|
|
|
|
|
|
for k in list(data.keys()):
|
2024-02-06 11:51:34 -05:00
|
|
|
if data[k] == defaults[k]:
|
2023-07-31 22:13:29 -04:00
|
|
|
del data[k]
|
|
|
|
|
2024-02-06 11:51:34 -05:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def generate_preset_yaml(state):
|
|
|
|
data = remove_defaults(state)
|
2023-06-13 19:34:35 -04:00
|
|
|
return yaml.dump(data, sort_keys=False)
|