File size: 2,212 Bytes
cf814c5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Offline pointwise FinConRank scorer; no generation and no fusion."""
import argparse
import json
from pathlib import Path

import torch
from peft import PeftModel
from transformers import AutoProcessor, Qwen2VLForConditionalGeneration
from financial_constraints import extract_constraints
from scoring import score_batch


def load(base, adapter):
    processor = AutoProcessor.from_pretrained(base, local_files_only=True)
    processor.image_processor.max_pixels = 602112
    model = Qwen2VLForConditionalGeneration.from_pretrained(
        base, torch_dtype=torch.float16, local_files_only=True, attn_implementation='sdpa')
    model = PeftModel.from_pretrained(model, adapter, local_files_only=True).to('cuda').eval()
    tokens = [processor.tokenizer.convert_tokens_to_ids(t) for t in ('True', 'False')]
    assert all(isinstance(t, int) for t in tokens) and tokens[0] != tokens[1]
    return processor, model, tokens


def main():
    p = argparse.ArgumentParser(description=__doc__)
    p.add_argument('--base-model', required=True, help='Explicit existing local Qwen base snapshot')
    p.add_argument('--adapter', required=True)
    p.add_argument('--query', required=True)
    p.add_argument('--image', nargs='+', required=True)
    p.add_argument('--constraints-json', help='Optional explicit constraints JSON; otherwise deterministic query-only parser')
    p.add_argument('--output', required=True)
    args = p.parse_args()
    constraints = json.loads(Path(args.constraints_json).read_text()) if args.constraints_json else extract_constraints(args.query)
    processor, model, tokens = load(args.base_model, args.adapter)
    torch.cuda.reset_peak_memory_stats()
    # Batch size one is deliberate; original-vs-copy validation uses the same setting.
    scores = [score_batch(processor, model, tokens, args.query, constraints, [Path(image)])[0] for image in args.image]
    Path(args.output).write_text(json.dumps({'scores': scores, 'constraints': constraints, 'token_ids': tokens,
        'peak_allocated_bytes': torch.cuda.max_memory_allocated(), 'max_pixels': 602112,
        'dtype': 'float16', 'batch_size': 1}, ensure_ascii=False, indent=2) + '\n')


if __name__ == '__main__':
    main()