-
Notifications
You must be signed in to change notification settings - Fork 16
/
exllama.py
385 lines (327 loc) · 11.1 KB
/
exllama.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import gc
import json
import random
from pathlib import Path
from time import time
from exllamav2 import (
ExLlamaV2,
ExLlamaV2Cache,
ExLlamaV2Cache_Q4,
ExLlamaV2Cache_Q6,
ExLlamaV2Cache_Q8,
ExLlamaV2Config,
ExLlamaV2Tokenizer,
)
from exllamav2.generator import (
ExLlamaV2DynamicGenerator,
ExLlamaV2DynamicJob,
ExLlamaV2Sampler,
)
from jinja2 import Template
from comfy.model_management import soft_empty_cache, unload_all_models
from comfy.utils import ProgressBar
from folder_paths import add_model_folder_path, get_folder_paths, models_dir
_CATEGORY = "zuellni/exllama"
_MAPPING = "ZuellniExLlama"
class Loader:
_input_info = None
@classmethod
def INPUT_TYPES(cls):
def get_input_info(cls):
add_model_folder_path("llm", str(Path(models_dir) / "llm"))
for folder in get_folder_paths("llm"):
for path in Path(folder).rglob("*/"):
if (path / "config.json").is_file():
parent = path.relative_to(folder).parent
cls._MODELS[str(parent / path.name)] = path
models = list(cls._MODELS.keys())
caches = list(cls._CACHES.keys())
default = models[0] if models else None
return models, caches, default
if Loader._input_info is None:
Loader._input_info = get_input_info(cls)
models, caches, default = Loader._input_info
return {
"required": {
"model": (models, {"default": default}),
"cache_bits": (caches, {"default": 4}),
"fast_tensors": ("BOOLEAN", {"default": True}),
"flash_attention": ("BOOLEAN", {"default": True}),
"max_seq_len": (
"INT",
{"default": 2048, "min": 0, "max": 2**20, "step": 256},
),
}
}
_CACHES = {
4: lambda m: ExLlamaV2Cache_Q4(m, lazy=True),
6: lambda m: ExLlamaV2Cache_Q6(m, lazy=True),
8: lambda m: ExLlamaV2Cache_Q8(m, lazy=True),
16: lambda m: ExLlamaV2Cache(m, lazy=True),
}
_MODELS = {}
CATEGORY = _CATEGORY
FUNCTION = "setup"
RETURN_NAMES = ("MODEL",)
RETURN_TYPES = ("EXL_MODEL",)
def setup(self, model, cache_bits, fast_tensors, flash_attention, max_seq_len):
self.unload()
self.cache_bits = cache_bits
self.config = ExLlamaV2Config(__class__._MODELS[model])
self.config.fasttensors = fast_tensors
self.config.no_flash_attn = not flash_attention
if max_seq_len:
self.config.max_seq_len = max_seq_len
if self.config.max_input_len > max_seq_len:
self.config.max_input_len = max_seq_len
self.config.max_attention_size = max_seq_len**2
self.tokenizer = ExLlamaV2Tokenizer(self.config)
return (self,)
def load(self):
if (
hasattr(self, "model")
and hasattr(self, "cache")
and hasattr(self, "generator")
and self.model
and self.cache
and self.generator
):
return
self.model = ExLlamaV2(self.config)
self.cache = __class__._CACHES[self.cache_bits](self.model)
progress = ProgressBar(len(self.model.modules))
self.model.load_autosplit(self.cache, callback=lambda _, __: progress.update(1))
self.generator = ExLlamaV2DynamicGenerator(
model=self.model,
cache=self.cache,
tokenizer=self.tokenizer,
paged=not self.config.no_flash_attn,
)
def unload(self):
if hasattr(self, "model") and self.model:
self.model.unload()
self.model = None
self.cache = None
self.generator = None
gc.collect()
soft_empty_cache()
class Formatter:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("EXL_MODEL",),
"messages": ("EXL_MESSAGES",),
"add_assistant_role": ("BOOLEAN", {"default": True}),
}
}
CATEGORY = _CATEGORY
FUNCTION = "format"
RETURN_NAMES = ("TEXT",)
RETURN_TYPES = ("STRING",)
def raise_exception(self, message):
raise Exception(message)
def render(self, template, messages, add_assistant_role):
return (
template.render(
add_generation_prompt=add_assistant_role,
raise_exception=self.raise_exception,
messages=messages,
bos_token="",
),
)
def format(self, model, messages, add_assistant_role):
template = model.tokenizer.tokenizer_config_dict["chat_template"]
template = Template(template)
try:
return self.render(template, messages, add_assistant_role)
except:
system = None
merged = []
for message in messages:
if message["role"] == "system":
system = {"role": "user", "content": message["content"]}
merged.append(system)
elif system and message["role"] == "user":
index = merged.index(system)
merged[index]["content"] += "\n" + message["content"]
system = None
else:
merged.append(message)
system = None
return self.render(template, merged, add_assistant_role)
class Tokenizer:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("EXL_MODEL",),
"text": ("STRING", {"default": "", "forceInput": True}),
"add_bos_token": ("BOOLEAN", {"default": True}),
"encode_special_tokens": ("BOOLEAN", {"default": True}),
}
}
CATEGORY = _CATEGORY
FUNCTION = "tokenize"
RETURN_NAMES = ("TOKENS",)
RETURN_TYPES = ("EXL_TOKENS",)
def tokenize(self, model, text, add_bos_token, encode_special_tokens):
return (
model.tokenizer.encode(
text=text,
add_bos=add_bos_token,
encode_special_tokens=encode_special_tokens,
),
)
class Settings:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"temperature": (
"FLOAT",
{"default": 1.0, "min": 0.0, "max": 10.0, "step": 0.01},
),
"penalty": (
"FLOAT",
{"default": 1.0, "min": 1.0, "max": 10.0, "step": 0.01},
),
"top_k": ("INT", {"default": 1, "min": 0, "max": 1000}),
"top_p": (
"FLOAT",
{"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01},
),
"top_a": (
"FLOAT",
{"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01},
),
"min_p": (
"FLOAT",
{"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01},
),
"tfs": (
"FLOAT",
{"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01},
),
"typical": (
"FLOAT",
{"default": 0.0, "min": 0.0, "max": 1.0, "step": 0.01},
),
"temperature_last": ("BOOLEAN", {"default": True}),
}
}
CATEGORY = _CATEGORY
FUNCTION = "set"
RETURN_NAMES = ("SETTINGS",)
RETURN_TYPES = ("EXL_SETTINGS",)
def set(
self,
temperature,
penalty,
top_k,
top_p,
top_a,
min_p,
tfs,
typical,
temperature_last,
):
settings = ExLlamaV2Sampler.Settings()
settings.temperature = temperature
settings.token_repetition_penalty = penalty
settings.top_k = top_k
settings.top_p = top_p
settings.top_a = top_a
settings.min_p = min_p
settings.tfs = tfs
settings.typical = typical
settings.temperature_last = temperature_last
return (settings,)
class Generator:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("EXL_MODEL",),
"tokens": ("EXL_TOKENS",),
"unload": ("BOOLEAN", {"default": False}),
"stop_conditions": ("STRING", {"default": r'"\n"'}),
"max_tokens": ("INT", {"default": 128, "min": 0, "max": 2**20}),
"seed": ("INT", {"default": 0, "min": 0, "max": 2**64 - 1}),
},
"optional": {"settings": ("EXL_SETTINGS",)},
}
CATEGORY = _CATEGORY
FUNCTION = "generate"
RETURN_NAMES = ("TEXT",)
RETURN_TYPES = ("STRING",)
def generate(
self,
model,
tokens,
unload,
stop_conditions,
max_tokens,
seed,
settings=None,
):
if unload:
unload_all_models()
model.unload()
model.load()
random.seed(seed)
tokens_len = tokens.shape[-1]
max_len = model.config.max_seq_len - tokens_len
stop = [model.tokenizer.eos_token_id]
if not max_tokens or max_tokens > max_len:
max_tokens = max_len
if stop_conditions.strip():
stop_conditions = json.loads(f"[{stop_conditions}]")
stop.extend(stop_conditions)
if not settings:
settings = ExLlamaV2Sampler.Settings()
settings.greedy()
job = ExLlamaV2DynamicJob(
input_ids=tokens,
max_new_tokens=max_tokens,
stop_conditions=stop,
gen_settings=settings,
)
progress = ProgressBar(max_tokens)
model.generator.enqueue(job)
start = time()
eos = False
chunks = []
count = 0
while not eos:
for response in model.generator.iterate():
if response["stage"] == "streaming":
chunk = response.get("text", "")
eos = response["eos"]
chunks.append(chunk)
progress.update(1)
count += 1
output = "".join(chunks)
total = round(time() - start, 2)
speed = round(count / total, 2)
print(
f"Output generated in {total} seconds",
f"({tokens_len} context, {count} tokens, {speed}t/s)",
)
if unload:
model.unload()
return (output,)
NODE_CLASS_MAPPINGS = {
f"{_MAPPING}Loader": Loader,
f"{_MAPPING}Formatter": Formatter,
f"{_MAPPING}Tokenizer": Tokenizer,
f"{_MAPPING}Settings": Settings,
f"{_MAPPING}Generator": Generator,
}
NODE_DISPLAY_NAME_MAPPINGS = {
f"{_MAPPING}Loader": "Loader",
f"{_MAPPING}Formatter": "Formatter",
f"{_MAPPING}Tokenizer": "Tokenizer",
f"{_MAPPING}Settings": "Settings",
f"{_MAPPING}Generator": "Generator",
}