|
3 | 3 | import re
|
4 | 4 |
|
5 | 5 | from dataclasses import dataclass, field
|
6 |
| -from typing import List |
7 |
| - |
8 |
| -# Based on https://github.com/ggerganov/llama.cpp/blob/master/examples/common.cpp |
| 6 | +from typing import List, Sequence, Tuple |
| 7 | +import typing |
9 | 8 |
|
| 9 | +# Based on https://github.com/ggerganov/llama.cpp/blob/master/common/common.cpp |
| 10 | +# and https://github.com/ggerganov/llama.cpp/blob/master/common/arg.cpp |
10 | 11 |
|
11 | 12 | @dataclass
|
12 | 13 | class GptParams:
|
@@ -40,8 +41,8 @@ class GptParams:
|
40 | 41 | input_suffix: str = ""
|
41 | 42 | antiprompt: List[str] = field(default_factory=list)
|
42 | 43 |
|
43 |
| - lora_adapter: str = "" |
44 |
| - lora_base: str = "" |
| 44 | + lora: List[str] = None |
| 45 | + lora_scaled: List[Tuple[str, float]] = None |
45 | 46 |
|
46 | 47 | memory_f16: bool = True
|
47 | 48 | random_prompt: bool = False
|
@@ -257,16 +258,56 @@ def gpt_params_parse(argv=None):
|
257 | 258 | parser.add_argument(
|
258 | 259 | "--lora",
|
259 | 260 | type=str,
|
260 |
| - default="", |
261 |
| - help="apply LoRA adapter (implies --no-mmap)", |
262 |
| - dest="lora_adapter", |
263 |
| - ) |
264 |
| - parser.add_argument( |
265 |
| - "--lora-base", |
266 |
| - type=str, |
267 |
| - default="", |
268 |
| - help="optional model to use as a base for the layers modified by the LoRA adapter", |
269 |
| - dest="lora_base", |
| 261 | + action="append", |
| 262 | + default=[], |
| 263 | + help="path to LoRA adapter (can be repeated to use multiple adapters)", |
| 264 | + metavar="FNAME", |
| 265 | + dest="lora", |
| 266 | + ) |
| 267 | + |
| 268 | + class MultiTupleAction(argparse.Action): |
| 269 | + """Action for handling multiple arguments as tuples with type conversion""" |
| 270 | + def __init__(self, |
| 271 | + option_strings: Sequence[str], |
| 272 | + dest: str, |
| 273 | + nargs: int = None, |
| 274 | + type: Tuple = None, |
| 275 | + metavar: Tuple = None, |
| 276 | + **kwargs): |
| 277 | + self.tuple_type = type |
| 278 | + super().__init__( |
| 279 | + option_strings=option_strings, |
| 280 | + dest=dest, |
| 281 | + type=str, # We will fix |
| 282 | + nargs=nargs, |
| 283 | + metavar=metavar, |
| 284 | + **kwargs |
| 285 | + ) |
| 286 | + |
| 287 | + def __call__(self, parser, namespace, values, option_string=None): |
| 288 | + if len(values) != self.nargs: |
| 289 | + parser.error( |
| 290 | + f'{option_string} requires {len(self.metavar)} arguments: ' |
| 291 | + f'{" ".join(self.metavar)}' |
| 292 | + ) |
| 293 | + |
| 294 | + converted_values = tuple(value_type(value) for value_type, value in zip(typing.get_args(self.tuple_type), values)) |
| 295 | + # Initialize list if needed |
| 296 | + if not hasattr(namespace, self.dest): |
| 297 | + setattr(namespace, self.dest, []) |
| 298 | + |
| 299 | + # Add the converted tuple to the list |
| 300 | + getattr(namespace, self.dest).append(converted_values) |
| 301 | + |
| 302 | + parser.add_argument( |
| 303 | + "--lora-scaled", |
| 304 | + action=MultiTupleAction, |
| 305 | + nargs=2, |
| 306 | + type=Tuple[str, float], |
| 307 | + help="path to LoRA adapter with user defined scaling (can be repeated to use multiple adapters)", |
| 308 | + metavar=('FNAME', 'SCALE'), |
| 309 | + dest='lora_scaled', |
| 310 | + default=[], |
270 | 311 | )
|
271 | 312 |
|
272 | 313 | parser.add_argument(
|
@@ -375,9 +416,6 @@ def gpt_params_parse(argv=None):
|
375 | 416 | delattr(args, "logit_bias_str")
|
376 | 417 | params = GptParams(**vars(args))
|
377 | 418 |
|
378 |
| - if params.lora_adapter: |
379 |
| - params.use_mmap = False |
380 |
| - |
381 | 419 | if logit_bias_str != None:
|
382 | 420 | for i in logit_bias_str:
|
383 | 421 | if m := re.match(r"(\d+)([-+]\d+)", i):
|
|
0 commit comments