File size: 1,759 Bytes
3bf7abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Make a lalamo-generated config.json loadable by uzu.

uzu config structs are generated by `#[uzu_config]`, which applies:
  * `#[serde(deny_unknown_fields)]` on every config struct
  * a required deserializer on every field, so `Option<T>` must still be present

lalamo emits a newer token_codec schema, so:
  * drop fields uzu does not know (`response_format`, ...)
  * add missing nullable fields (`output_parser_regex`, `end_of_thinking_tag`)

Usage:
  python scripts/normalize_lalamo_config.py /path/to/model_dir [more_dirs...]
"""

from __future__ import annotations

import argparse
import json
from pathlib import Path

REQUIRED = ("output_parser_regex", "end_of_thinking_tag")
UNSUPPORTED = ("response_format", "reasoning_effort")


def normalize(output_dir: Path) -> bool:
    path = output_dir / "config.json"
    config = json.loads(path.read_text())
    token_codec = config.get("token_codec_config")
    if token_codec is None:
        print(f"{output_dir}: no token_codec_config, skipped")
        return False

    removed = [key for key in UNSUPPORTED if token_codec.pop(key, None) is not None]
    added = [key for key in REQUIRED if key not in token_codec]
    for key in added:
        token_codec[key] = None

    if not removed and not added:
        print(f"{output_dir}: already normalized")
        return False

    path.write_text(json.dumps(config, indent=2) + "\n")
    print(f"{output_dir}: removed={removed or '[]'} added={added or '[]'}")
    return True


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("dirs", type=Path, nargs="+")
    args = parser.parse_args()
    for directory in args.dirs:
        normalize(directory)


if __name__ == "__main__":
    main()