Model: Xirui1208/selective-readtwice-7b-early-evaluated-20260928 Source: Original Platform
30 lines
2.0 KiB
Python
30 lines
2.0 KiB
Python
"""Read a local long document with the calibrated selective RL gate."""
|
|
import argparse,asyncio,json
|
|
from pathlib import Path
|
|
from runtime import ROOT,CFG,Reader,tokenizer,atomic_json
|
|
from chunking import split_context
|
|
from gate import Gate,predict
|
|
from evaluate import select_chunks
|
|
|
|
async def main(args):
|
|
adapter=Path(args.adapter).resolve()
|
|
selection=json.loads((adapter/'selection.json').read_text()) if (adapter/'selection.json').exists() else {}
|
|
if args.threshold is not None: selection['threshold']=args.threshold
|
|
if 'threshold' not in selection: raise ValueError('This checkpoint has no selected threshold; supply --threshold explicitly')
|
|
tok=tokenizer(); text=Path(args.context_file).read_text()
|
|
ex={'id':'inference','question':args.question,**split_context(tok,text,CFG['chunk_tokens'])}
|
|
async with Reader(tok) as reader:
|
|
prepared=await reader.outlines(ex); prepared['id']=ex['id']
|
|
gate=Gate(adapter,trainable=False); probs=predict(gate,[prepared])[ex['id']]
|
|
chosen=select_chunks(probs,selection['threshold'],prepared['force_read_ids'])
|
|
result=await reader.rollout(ex,prepared,chosen,trace=args.trace)
|
|
for key in ['em','f1','sub_em']: result.pop(key,None)
|
|
result.update({'gate_probabilities':probs,'threshold':selection['threshold'],'adapter':str(adapter),
|
|
'first_pass_read_all_chunks':True,'global_chunk_ids_are_zero_based':True,'source_tokens':ex['num_tokens'],
|
|
'pages':[{'global_ids':p['global_ids'],'gate_input_tokens':p['input_tokens']} for p in prepared['pages']]})
|
|
if args.output: atomic_json(args.output,result)
|
|
print(json.dumps(result,ensure_ascii=False,indent=2))
|
|
|
|
if __name__=='__main__':
|
|
p=argparse.ArgumentParser(); p.add_argument('--adapter',default=str(ROOT.parent/'gate_adapter')); p.add_argument('--context-file',required=True); p.add_argument('--question',required=True); p.add_argument('--output'); p.add_argument('--threshold',type=float); p.add_argument('--trace',action='store_true'); asyncio.run(main(p.parse_args()))
|