File size: 1,828 Bytes
4eee182
c4a11dd
4eee182
c4a11dd
 
 
 
 
 
 
 
 
 
 
 
4eee182
c4a11dd
 
 
 
 
 
 
 
 
 
 
 
 
4eee182
 
 
c4a11dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Export a tensor-only checkpoint as a Manekko model repository.



Usage:

    python convert_checkpoint.py /path/student.ckpt --output ./export

"""

from __future__ import annotations

import argparse
import importlib.util
from pathlib import Path
import sys


def _load_package(folder: Path):
    """Load local model code for direct script execution."""
    name = "_manekko_export_code"
    spec = importlib.util.spec_from_file_location(
        name, folder / "__init__.py", submodule_search_locations=[str(folder)]
    )
    module = importlib.util.module_from_spec(spec)
    sys.modules[name] = module
    spec.loader.exec_module(module)
    return module


def main(argv=None):
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("checkpoint", type=Path)
    parser.add_argument(
        "--config", type=Path, default=Path(__file__).parent / "config.json"
    )
    parser.add_argument("--output", type=Path, required=True)
    args = parser.parse_args(argv)
    if not args.checkpoint.is_file():
        parser.error(f"Checkpoint does not exist: {args.checkpoint}")
    if not args.config.is_file():
        parser.error(f"Configuration does not exist: {args.config}")
    package = _load_package(Path(__file__).resolve().parent)
    from _manekko_export_code.modeling_manekko import load_encoder_file

    config = package.ManekkoConfig.from_json_file(args.config)
    model = package.ManekkoModel(config)
    state = load_encoder_file(args.checkpoint)
    model.load_state_dict(state, strict=True)
    model.eval().save_pretrained(args.output)
    print(f"Exported {len(state)} encoder tensors to {args.output.resolve()}")
    print("Upload all exported files to the matching model repository.")


if __name__ == "__main__":
    main()