File size: 1,082 Bytes
fc43746
4c87263
 
 
 
 
 
 
 
 
 
fc43746
4c87263
 
 
fc43746
 
 
4c87263
 
 
fc43746
 
4c87263
 
 
 
 
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
"""Persistent JSONL inference for the v2 standalone candidate."""
import argparse
import json
from pathlib import Path
import sys


def main():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument('--model', default=str(Path(__file__).resolve().parent))
    p.add_argument('--mode', choices=('default', 'flash'), default='default')
    p.add_argument('--input', type=Path)
    a = p.parse_args()
    import torch
    from transformers import AutoModel
    torch.set_num_threads(4); torch.cuda.set_device(0); torch.backends.cuda.enable_cudnn_sdp(False)
    model = AutoModel.from_pretrained(a.model, trust_remote_code=True,
        local_files_only=Path(a.model).is_dir(), dtype=torch.bfloat16, device_map={'': 'cuda:0'}).eval()
    _ = model.processor
    stream = a.input.open() if a.input else sys.stdin
    try:
        for line in stream:
            if line.strip(): print(json.dumps(model.decide(json.loads(line), a.mode), ensure_ascii=False, allow_nan=False), flush=True)
    finally:
        if a.input: stream.close()


if __name__ == '__main__': main()