uzu-quant-kit / scripts /normalize_lalamo_config.py
yamahei21's picture
Add rewrite_lalamo_hybrid_spec and kit completeness
3bf7abb verified
Raw History Blame Contribute Delete
1.76 kB
#!/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()