File size: 2,824 Bytes
9126e0d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Paired warmed latency, same images/GPU, balanced plan order, no GT use."""
from pathlib import Path
import sys,json,time,os
import numpy as np
from PIL import Image
ROOT=Path(__file__).resolve().parents[1];from project_paths import legacy_root;OLD=legacy_root(ROOT);sys.path.insert(0,str(OLD/'code'))
from infer import ClipSeg,GroundedSam
import phase1_repair as p
from r2_repair import primary_plan,primary_mask,repair
import torch
torch.set_num_threads(4)
rows=[json.loads(s) for s in (ROOT/'data/rich_synthetic.jsonl').read_text().splitlines()]
chosen=[r for r in rows if r['render']=='rich' and r['mode']=='relational' and r['endpoint']==0][:12]
output=[]
for model,cls in [('clipseg',ClipSeg),('groundedsam',GroundedSam)]:
    engine=cls()
    image=Image.open(ROOT/chosen[0]['image_path']).convert('RGB');q=p.parse_query(chosen[0]['query']);engine.predict(image,p.query_plan(chosen[0]['query'])[1]);torch.cuda.synchronize()
    for i,r in enumerate(chosen):
        image=Image.open(ROOT/r['image_path']).convert('RGB');q=p.parse_query(r['query'])
        plans={'frozen':[q.original],'sfap':primary_plan(r['query']),'cacp':p.query_plan(r['query'])[1]}
        for rep in range(3):
            order=['frozen','sfap','cacp'];shift=(i+rep)%3;order=order[shift:]+order[:shift]
            for variant in order:
                torch.cuda.synchronize();start=time.perf_counter();maps,stats=engine.predict(image,plans[variant]);torch.cuda.synchronize();forward=time.perf_counter()-start
                maps={k:v.astype(np.float16).astype(np.float32) for k,v in maps.items()}
                start=time.perf_counter()
                if variant=='frozen':mask=p.native(maps[q.original],engine.threshold)
                elif variant=='sfap':mask=primary_mask(r['query'],maps,engine.threshold)
                else:mask=repair(r['query'],maps,engine.threshold)[0]['cacp']
                cpu=time.perf_counter()-start
                if variant=='cacp':assert np.array_equal(primary_mask(r['query'],maps,engine.threshold),repair(r['query'],maps,engine.threshold)[0]['safe_anchor'])
                output.append(dict(model=model,image=r['image_path'],scene_id=r['scene_id'],repeat=rep,order=order,variant=variant,queries=len(plans[variant]),forward_seconds=forward,postprocess_seconds=cpu,total_seconds=forward+cpu))
        print('TIMING',model,i+1,len(chosen),flush=True)
    del engine;torch.cuda.empty_cache()
dest=ROOT/'results/paired_timing.json';dest.write_text(json.dumps(dict(gpu=torch.cuda.get_device_name(0),slurm_job_id=os.getenv('SLURM_JOB_ID'),records=output,warmup='One complete four-query plan per model before timing',cache='Disabled',repeats=3,selection='First twelve rich relational scene records, text/ID selection; no label or score selection'),indent=2))
print('PAIRED_TIMING_COMPLETE',flush=True)