Spaces:
Sleeping

decision-model / tests /test_holdout_protocol.py
xstard's picture
Implement encoder-only bilingual decision API and demo
b6ae5c2 verified
Raw History Blame Contribute Delete
4.92 kB
import json
from pathlib import Path
import numpy as np
import pytest
from decision_model.budget import BudgetLedger
from decision_model.learning import save_bundle
from decision_model.artifacts import sha256
from decision_model.settings import ENCODER_ID,ENCODER_REVISION
def checkpoint(path):
manifest={'encoder_id':ENCODER_ID,'encoder_revision':ENCODER_REVISION,'hidden_size':2,'stage':'trained',
'head_layout':'shared','training':{'method':'test'},'data_hashes':{'train':'a','validation':'b'}}
save_bundle(path,np.ones(2),dict.fromkeys(['noul','choice','score'],1.),manifest,{'stage':'trained'})
def test_lock_is_created_before_generation_and_cannot_be_rebound_on_resume(tmp_path):
from decision_model.holdout import bind_selection_lock
model=tmp_path/'model';checkpoint(model)
lock={'checkpoint':str(model),'immutable_files':{'head.safetensors':sha256(model/'head.safetensors')},'selected_at':'before-test'}
saved=tmp_path/'holdout-lock.json';protocol={'seeds_sha256':'1'*64,'rubric_sha256':'2'*64}
bind_selection_lock(saved,lock,protocol)
original=saved.read_bytes()
assert bind_selection_lock(saved,lock,protocol)
for changed,params in [(dict(lock,immutable_files={}),protocol),(dict(lock,selected_at='after-test'),protocol),
(lock,dict(protocol,seeds_sha256='3'*64))]:
with pytest.raises(ValueError):bind_selection_lock(saved,changed,params)
assert saved.read_bytes()==original
def test_adapted_checkpoint_lock_cannot_omit_encoder_delta(tmp_path):
from safetensors.numpy import save_file
from decision_model.holdout import bind_selection_lock
model=tmp_path/'model';model.mkdir()
save_file({'x':np.ones(1)},str(model/'encoder_delta.safetensors'))
manifest={'encoder_id':ENCODER_ID,'encoder_revision':ENCODER_REVISION,'hidden_size':2,'stage':'trained',
'encoder_delta':{'layers':[21],'parameter_names':['x']},'training':{},'data_hashes':{'train':'a','validation':'b'}}
save_bundle(model,np.ones(2),dict.fromkeys(['noul','choice','score'],1.),manifest,{'stage':'trained'})
lock={'checkpoint':str(model),'immutable_files':{'head.safetensors':sha256(model/'head.safetensors')},'selected_at':'before'}
with pytest.raises(ValueError):bind_selection_lock(tmp_path/'lock.json',lock,{})
assert not (tmp_path/'lock.json').exists()
def test_lifetime_ledger_missing_or_reset_fails_closed(tmp_path):
from decision_model.holdout import verify_lifetime_budget
path=tmp_path/'cost.sqlite';ledger=BudgetLedger(path,5)
ledger.reserve('original',2);ledger.settle('original',1.72725)
anchor=tmp_path/'anchor.json'
anchor.write_text(json.dumps({'cap_micros':5000000,'settled_calls':{'original':1727250}}))
verify_lifetime_budget(path,anchor)
path.unlink()
with pytest.raises((ValueError,FileNotFoundError)):verify_lifetime_budget(path,anchor)
BudgetLedger(path,5)
with pytest.raises(ValueError):verify_lifetime_budget(path,anchor)
def test_existing_only_ledger_never_creates_a_missing_file(tmp_path):
path=tmp_path/'missing.sqlite'
with pytest.raises(FileNotFoundError):BudgetLedger(path,5,existing_only=True)
assert not path.exists()
def test_generated_text_overlap_rejects_both_language_siblings():
from decision_model.holdout import isolate_fresh_families
from decision_model.data import Ticket
def ticket(i,lang,text):
return Ticket(id=f'{i}:{lang}',group_id=i,language=lang,text=text,department='billing',
wants_refund=True,frustration=0,source='test',license='MIT')
old=[ticket('old','en','Please return the money for the duplicate payment charged to my account yesterday')]
fresh=[ticket('new','en','Kindly return the money for the duplicate payment charged to my account yesterday'),
ticket('new','uk','Це інший переклад тієї самої групи')]
kept,rejected=isolate_fresh_families(old,fresh)
assert kept==[] and {t.id for t in rejected}=={'new:en','new:uk'}
def test_fresh_near_duplicate_sources_form_one_group_across_translations():
from decision_model.holdout import isolate_fresh_families
from decision_model.data import Ticket
def ticket(i,lang,text):
return Ticket(id=f'{i}:{lang}',group_id=i,language=lang,text=text,department='billing',
wants_refund=True,frustration=0,source='test',license='MIT')
fresh=[ticket('a','en','Please return the money for the duplicate payment charged to my account yesterday'),
ticket('b','en','Kindly return the money for the duplicate payment charged to my account yesterday'),
ticket('a','uk','Перший переклад'),ticket('b','uk','Інший переклад')]
kept,rejected=isolate_fresh_families([],fresh)
assert len(kept)==4 and not rejected
assert len({t.group_id for t in kept})==1