2023-09-29 04:19:47 +02:00
|
|
|
import json
|
2023-06-17 00:00:37 +02:00
|
|
|
import re
|
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
import yaml
|
|
|
|
|
2023-09-11 23:49:30 +02:00
|
|
|
from modules import loaders, metadata_gguf, shared, ui
|
2023-06-17 00:00:37 +02:00
|
|
|
|
|
|
|
|
2023-09-11 23:49:30 +02:00
|
|
|
def get_fallback_settings():
|
|
|
|
return {
|
|
|
|
'wbits': 'None',
|
|
|
|
'groupsize': 'None',
|
2023-09-29 15:14:16 +02:00
|
|
|
'desc_act': False,
|
|
|
|
'model_type': 'None',
|
2023-09-29 04:19:47 +02:00
|
|
|
'max_seq_len': 2048,
|
2023-09-11 23:49:30 +02:00
|
|
|
'n_ctx': 2048,
|
|
|
|
'rope_freq_base': 0,
|
2023-09-12 22:02:42 +02:00
|
|
|
'compress_pos_emb': 1,
|
2023-09-29 15:14:16 +02:00
|
|
|
'truncation_length': shared.settings['truncation_length'],
|
|
|
|
'skip_special_tokens': shared.settings['skip_special_tokens'],
|
|
|
|
'custom_stopping_strings': shared.settings['custom_stopping_strings'],
|
2023-09-11 23:49:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def get_model_metadata(model):
|
2023-06-17 00:00:37 +02:00
|
|
|
model_settings = {}
|
2023-09-11 23:49:30 +02:00
|
|
|
|
|
|
|
# Get settings from models/config.yaml and models/config-user.yaml
|
|
|
|
settings = shared.model_config
|
2023-06-17 00:00:37 +02:00
|
|
|
for pat in settings:
|
|
|
|
if re.match(pat.lower(), model.lower()):
|
|
|
|
for k in settings[pat]:
|
|
|
|
model_settings[k] = settings[pat][k]
|
|
|
|
|
2023-09-11 23:49:30 +02:00
|
|
|
if 'loader' not in model_settings:
|
|
|
|
loader = infer_loader(model, model_settings)
|
|
|
|
if 'wbits' in model_settings and type(model_settings['wbits']) is int and model_settings['wbits'] > 0:
|
|
|
|
loader = 'AutoGPTQ'
|
|
|
|
|
|
|
|
model_settings['loader'] = loader
|
|
|
|
|
|
|
|
# Read GGUF metadata
|
|
|
|
if model_settings['loader'] in ['llama.cpp', 'llamacpp_HF', 'ctransformers']:
|
|
|
|
path = Path(f'{shared.args.model_dir}/{model}')
|
|
|
|
if path.is_file():
|
|
|
|
model_file = path
|
|
|
|
else:
|
|
|
|
model_file = list(path.glob('*.gguf'))[0]
|
|
|
|
|
|
|
|
metadata = metadata_gguf.load_metadata(model_file)
|
|
|
|
if 'llama.context_length' in metadata:
|
|
|
|
model_settings['n_ctx'] = metadata['llama.context_length']
|
2023-09-12 22:02:42 +02:00
|
|
|
if 'llama.rope.scale_linear' in metadata:
|
|
|
|
model_settings['compress_pos_emb'] = metadata['llama.rope.scale_linear']
|
|
|
|
if 'llama.rope.freq_base' in metadata:
|
2023-09-14 21:15:52 +02:00
|
|
|
model_settings['rope_freq_base'] = metadata['llama.rope.freq_base']
|
|
|
|
|
2023-09-29 04:19:47 +02:00
|
|
|
else:
|
2023-09-29 15:14:16 +02:00
|
|
|
# Read transformers metadata
|
2023-09-29 04:19:47 +02:00
|
|
|
path = Path(f'{shared.args.model_dir}/{model}/config.json')
|
|
|
|
if path.exists():
|
|
|
|
metadata = json.loads(open(path, 'r').read())
|
|
|
|
if 'max_position_embeddings' in metadata:
|
|
|
|
model_settings['truncation_length'] = metadata['max_position_embeddings']
|
|
|
|
model_settings['max_seq_len'] = metadata['max_position_embeddings']
|
|
|
|
|
2023-09-29 15:14:16 +02:00
|
|
|
if 'rope_theta' in metadata:
|
|
|
|
model_settings['rope_freq_base'] = metadata['rope_theta']
|
|
|
|
|
|
|
|
if 'rope_scaling' in metadata and type(metadata['rope_scaling']) is dict and all(key in metadata['rope_scaling'] for key in ('type', 'factor')):
|
|
|
|
if metadata['rope_scaling']['type'] == 'linear':
|
|
|
|
model_settings['compress_pos_emb'] = metadata['rope_scaling']['factor']
|
|
|
|
|
|
|
|
if 'quantization_config' in metadata:
|
|
|
|
if 'bits' in metadata['quantization_config']:
|
|
|
|
model_settings['wbits'] = metadata['quantization_config']['bits']
|
|
|
|
if 'group_size' in metadata['quantization_config']:
|
|
|
|
model_settings['groupsize'] = metadata['quantization_config']['group_size']
|
|
|
|
if 'desc_act' in metadata['quantization_config']:
|
|
|
|
model_settings['desc_act'] = metadata['quantization_config']['desc_act']
|
|
|
|
|
|
|
|
# Read AutoGPTQ metadata
|
|
|
|
path = Path(f'{shared.args.model_dir}/{model}/quantize_config.json')
|
|
|
|
if path.exists():
|
|
|
|
metadata = json.loads(open(path, 'r').read())
|
|
|
|
if 'bits' in metadata:
|
|
|
|
model_settings['wbits'] = metadata['bits']
|
|
|
|
if 'group_size' in metadata:
|
|
|
|
model_settings['groupsize'] = metadata['group_size']
|
|
|
|
if 'desc_act' in metadata:
|
|
|
|
model_settings['desc_act'] = metadata['desc_act']
|
|
|
|
|
2023-09-14 21:15:52 +02:00
|
|
|
# Apply user settings from models/config-user.yaml
|
|
|
|
settings = shared.user_config
|
|
|
|
for pat in settings:
|
|
|
|
if re.match(pat.lower(), model.lower()):
|
|
|
|
for k in settings[pat]:
|
|
|
|
model_settings[k] = settings[pat][k]
|
2023-09-11 23:49:30 +02:00
|
|
|
|
2023-06-17 00:00:37 +02:00
|
|
|
return model_settings
|
|
|
|
|
|
|
|
|
2023-09-11 23:49:30 +02:00
|
|
|
def infer_loader(model_name, model_settings):
|
2023-06-17 00:00:37 +02:00
|
|
|
path_to_model = Path(f'{shared.args.model_dir}/{model_name}')
|
|
|
|
if not path_to_model.exists():
|
|
|
|
loader = None
|
2023-09-29 04:32:35 +02:00
|
|
|
elif (path_to_model / 'quantize_config.json').exists() or ('wbits' in model_settings and type(model_settings['wbits']) is int and model_settings['wbits'] > 0):
|
2023-07-16 07:29:23 +02:00
|
|
|
loader = 'AutoGPTQ'
|
2023-10-05 18:19:18 +02:00
|
|
|
elif (path_to_model / 'quant_config.json').exists():
|
|
|
|
loader = 'AutoAWQ'
|
2023-09-11 16:30:56 +02:00
|
|
|
elif len(list(path_to_model.glob('*.gguf'))) > 0:
|
2023-06-17 00:00:37 +02:00
|
|
|
loader = 'llama.cpp'
|
2023-09-11 16:30:56 +02:00
|
|
|
elif re.match(r'.*\.gguf', model_name.lower()):
|
2023-06-17 00:00:37 +02:00
|
|
|
loader = 'llama.cpp'
|
2023-10-05 18:19:18 +02:00
|
|
|
elif re.match(r'.*-awq', model_name.lower()):
|
|
|
|
loader = 'AutoAWQ'
|
2023-08-07 02:49:27 +02:00
|
|
|
elif re.match(r'.*rwkv.*\.pth', model_name.lower()):
|
2023-06-17 00:00:37 +02:00
|
|
|
loader = 'RWKV'
|
2023-09-23 22:04:27 +02:00
|
|
|
elif re.match(r'.*exl2', model_name.lower()):
|
|
|
|
loader = 'ExLlamav2_HF'
|
2023-06-17 00:00:37 +02:00
|
|
|
else:
|
|
|
|
loader = 'Transformers'
|
|
|
|
|
|
|
|
return loader
|
|
|
|
|
|
|
|
|
|
|
|
# UI: update the command-line arguments based on the interface values
|
|
|
|
def update_model_parameters(state, initial=False):
|
|
|
|
elements = ui.list_model_elements() # the names of the parameters
|
|
|
|
gpu_memories = []
|
|
|
|
|
|
|
|
for i, element in enumerate(elements):
|
|
|
|
if element not in state:
|
|
|
|
continue
|
|
|
|
|
|
|
|
value = state[element]
|
|
|
|
if element.startswith('gpu_memory'):
|
|
|
|
gpu_memories.append(value)
|
|
|
|
continue
|
|
|
|
|
2023-09-19 22:11:46 +02:00
|
|
|
if initial and element in shared.provided_arguments:
|
2023-06-17 00:00:37 +02:00
|
|
|
continue
|
|
|
|
|
|
|
|
# Setting null defaults
|
|
|
|
if element in ['wbits', 'groupsize', 'model_type'] and value == 'None':
|
|
|
|
value = vars(shared.args_defaults)[element]
|
|
|
|
elif element in ['cpu_memory'] and value == 0:
|
|
|
|
value = vars(shared.args_defaults)[element]
|
|
|
|
|
|
|
|
# Making some simple conversions
|
|
|
|
if element in ['wbits', 'groupsize', 'pre_layer']:
|
|
|
|
value = int(value)
|
|
|
|
elif element == 'cpu_memory' and value is not None:
|
|
|
|
value = f"{value}MiB"
|
|
|
|
|
|
|
|
if element in ['pre_layer']:
|
|
|
|
value = [value] if value > 0 else None
|
|
|
|
|
|
|
|
setattr(shared.args, element, value)
|
|
|
|
|
|
|
|
found_positive = False
|
|
|
|
for i in gpu_memories:
|
|
|
|
if i > 0:
|
|
|
|
found_positive = True
|
|
|
|
break
|
|
|
|
|
|
|
|
if not (initial and vars(shared.args)['gpu_memory'] != vars(shared.args_defaults)['gpu_memory']):
|
|
|
|
if found_positive:
|
|
|
|
shared.args.gpu_memory = [f"{i}MiB" for i in gpu_memories]
|
|
|
|
else:
|
|
|
|
shared.args.gpu_memory = None
|
|
|
|
|
|
|
|
|
|
|
|
# UI: update the state variable with the model settings
|
|
|
|
def apply_model_settings_to_state(model, state):
|
2023-09-11 23:49:30 +02:00
|
|
|
model_settings = get_model_metadata(model)
|
|
|
|
if 'loader' in model_settings:
|
|
|
|
loader = model_settings.pop('loader')
|
2023-06-17 00:00:37 +02:00
|
|
|
|
2023-08-24 21:27:36 +02:00
|
|
|
# If the user is using an alternative loader for the same model type, let them keep using it
|
2023-09-12 19:44:26 +02:00
|
|
|
if not (loader == 'AutoGPTQ' and state['loader'] in ['GPTQ-for-LLaMa', 'ExLlama', 'ExLlama_HF', 'ExLlamav2', 'ExLlamav2_HF']) and not (loader == 'llama.cpp' and state['loader'] in ['llamacpp_HF', 'ctransformers']):
|
2023-06-17 00:00:37 +02:00
|
|
|
state['loader'] = loader
|
|
|
|
|
|
|
|
for k in model_settings:
|
|
|
|
if k in state:
|
2023-07-12 04:27:38 +02:00
|
|
|
if k in ['wbits', 'groupsize']:
|
|
|
|
state[k] = str(model_settings[k])
|
|
|
|
else:
|
|
|
|
state[k] = model_settings[k]
|
2023-06-17 00:00:37 +02:00
|
|
|
|
|
|
|
return state
|
|
|
|
|
|
|
|
|
|
|
|
# Save the settings for this model to models/config-user.yaml
|
|
|
|
def save_model_settings(model, state):
|
|
|
|
if model == 'None':
|
|
|
|
yield ("Not saving the settings because no model is loaded.")
|
|
|
|
return
|
|
|
|
|
|
|
|
with Path(f'{shared.args.model_dir}/config-user.yaml') as p:
|
|
|
|
if p.exists():
|
|
|
|
user_config = yaml.safe_load(open(p, 'r').read())
|
|
|
|
else:
|
|
|
|
user_config = {}
|
|
|
|
|
|
|
|
model_regex = model + '$' # For exact matches
|
|
|
|
if model_regex not in user_config:
|
|
|
|
user_config[model_regex] = {}
|
|
|
|
|
|
|
|
for k in ui.list_model_elements():
|
2023-08-01 15:10:09 +02:00
|
|
|
if k == 'loader' or k in loaders.loaders_and_params[state['loader']]:
|
|
|
|
user_config[model_regex][k] = state[k]
|
2023-09-14 21:15:52 +02:00
|
|
|
|
|
|
|
shared.user_config = user_config
|
2023-06-17 00:00:37 +02:00
|
|
|
|
2023-08-01 15:10:09 +02:00
|
|
|
output = yaml.dump(user_config, sort_keys=False)
|
2023-06-17 00:00:37 +02:00
|
|
|
with open(p, 'w') as f:
|
2023-08-01 15:10:09 +02:00
|
|
|
f.write(output)
|
2023-06-17 00:00:37 +02:00
|
|
|
|
|
|
|
yield (f"Settings for {model} saved to {p}")
|