DeciCo-RSI / tests /test_student_selection.py
juray0410's picture
Initial commit
1831ecd
Raw History Blame Contribute Delete
8.29 kB
"""Model choice is sealed and propagated to training, inference and seed workers."""
import copy
import json
import os
from pathlib import Path
import subprocess
import sys
import pytest
from common import read_json,write_json,digest
from modeling.student import configure,validate,MODELS_ROOT,DEFAULT
from test_release_integration import command,ROOT
@pytest.mark.parametrize('name',[DEFAULT])
def test_model_selection_preserves_teacher_and_rsi_rules(config,name):
before=copy.deepcopy(config)
selected=configure(config,name=name)
assert config==before
assert selected['student']=={'name':name,'path':MODELS_ROOT+'/'+name}
assert selected['serving_requirements']['student']['model']==name
for key in ('data_service','robust_gate','harness_search','nested_rsi'):
assert selected[key]==before[key]
assert selected['serving_requirements']['teacher']==before['serving_requirements']['teacher']
@pytest.mark.parametrize('name',[DEFAULT])
def test_fixed_student_prepare_and_preflight_without_weight_access(tmp_path,name):
campaign=tmp_path/name
result=command(ROOT/'prepare.py','--output',campaign,'--student-model',name,
'--initialization','latest_accepted')
assert result.returncode==0,result.stdout+result.stderr
config=read_json(campaign/'config.json')
assert config['student']['name']==name and config['student']['path']==MODELS_ROOT+'/'+name
result=command(campaign/'runtime/preflight_local.py','--config',campaign/'config.json',
'--output',campaign/'checks/offline.json')
assert result.returncode==0,result.stdout+result.stderr
report=read_json(campaign/'checks/offline.json')
assert report['student']==config['student'] and report['model_calls']==0
def test_explicit_model_path_and_root_are_supported(config):
assert configure(config,name='Qwen3.5-35B-A3B',models_root='/custom/Models')['student']['path']=='/custom/Models/Qwen3.5-35B-A3B'
chosen=configure(config,name='Qwen3.5-35B-A3B',model_path='/custom/renamed-weights')
assert configure(chosen)['student']==chosen['student']
assert chosen['student']['path']=='/custom/renamed-weights'
with pytest.raises(ValueError,match='absolute'):configure(config,name='Qwen3.5-35B-A3B',models_root='relative')
with pytest.raises(ValueError,match='Unsupported'):configure(config,name='wrong')
chosen['serving_requirements']['student']['model']='different-model'
with pytest.raises(ValueError,match='identity'):validate(chosen)
def test_bootstrap_overrides_stale_shell_and_binds_every_model_consumer(tmp_path,config):
cfg=configure(config,name='Qwen3.5-35B-A3B');file=tmp_path/'config.json';write_json(file,cfg)
code='''
import json,sys
sys.path.insert(0,sys.argv[1])
from modeling.student import bootstrap_cli
bootstrap_cli()
import common
from operators import model_rsi
from controller import seed_service,model_session
from modeling import client
client.configure_teacher(json.load(open(sys.argv[-1]))['data_service'])
assert common.BASE_MODEL_NAME==model_rsi.BASE_MODEL_NAME==seed_service.BASE_MODEL_NAME==client.BASE_MODEL_NAME=='Qwen3.5-35B-A3B'
assert str(common.BASE_MODEL)==str(model_rsi.BASE_MODEL)==str(model_session.BASE_MODEL)==str(seed_service.BASE_MODEL)
from types import SimpleNamespace
seen=[]
def completion(payload,backend):
seen.append((backend,payload))
return SimpleNamespace(status_code=200,json=lambda:{'choices':[{'message':{'role':'assistant','content':'[]'},'finish_reason':'stop'}],'usage':{}}),None
client._completion_request=completion
client.chat([{'role':'user','content':'request'}])
client.data_chat([{'role':'user','content':'request'}])
assert seen[0][1]['model']=='Qwen3.5-35B-A3B' and seen[0][1]['chat_template_kwargs']=={'enable_thinking':False}
assert seen[1][1]['model']=='Qwen3.8-27B'
print(json.dumps({'name':common.BASE_MODEL_NAME,'path':str(common.BASE_MODEL),'teacher':client.DATA_MODEL}))
'''
env={**os.environ,'PYTHONDONTWRITEBYTECODE':'1','DECICO_RSI_MODEL_NAME':'stale-model',
'DECICO_RSI_BASE_MODEL':'/stale/base'}
result=subprocess.run([sys.executable,'-B','-c',code,str(ROOT/'decico_rsi'),'--config',str(file)],
cwd=ROOT,env=env,text=True,capture_output=True,timeout=30)
assert result.returncode==0,result.stdout+result.stderr
assert json.loads(result.stdout)['path']==cfg['student']['path']
def test_fixed_student_parallel_suite_keeps_choice_for_every_seed(tmp_path):
suite=tmp_path/'small'
result=command(ROOT/'run_experiments.py','--output',suite,'--seeds',401,402,
'--student-model','Qwen3.5-35B-A3B','--initialization','baseline','--prepare-only')
assert result.returncode==0,result.stdout+result.stderr
assert read_json(suite/'suite.json')['student']['name']=='Qwen3.5-35B-A3B'
for seed in [401,402]:
cfg=read_json(suite/f'seed-{seed}/config.json')
assert cfg['student']['name']=='Qwen3.5-35B-A3B' and cfg['training']['gpu_min_free_mb']==60000
assert cfg['data_service']['model']=='Qwen3.8-27B'
assert cfg['training']['initialization']=='baseline'
def test_student_and_teacher_serving_use_preserved_parser():
from controller.seed_service import serving_command
for name in [DEFAULT, 'Qwen3.8-27B']:
cmd = serving_command(5058, 32768, model=MODELS_ROOT+'/'+name, model_name=name)
assert cmd[cmd.index('--tool-call-parser')+1] == 'qwen3_coder'
assert '--language-model-only' in cmd
def test_mixed_runtime_is_rejected_before_training(config):
from modeling.student import assert_runtime
with pytest.raises(ValueError,match='differs from sealed'):
assert_runtime(configure(config,model_path='/different/base'))
def test_parallel_worker_bootstraps_fixed_student_before_imports(tmp_path,config):
from controller.state_assets import fingerprint
from helpers import state
cfg=configure(config,name='Qwen3.5-35B-A3B');current={**state(tmp_path),'model':'Qwen3.5-35B-A3B'}
request={'state':current,'assets':fingerprint(current),'config':cfg,'rows':[],
'output':str(tmp_path/'episodes'),'endpoint':'http://localhost:5057/v1'}
path=tmp_path/'request.json';write_json(path,request)
code='''
import runpy,sys,types
from pathlib import Path
sys.path.insert(0,sys.argv[1])
def evaluate(rows,state,config,output):
import common
from modeling import client
assert common.BASE_MODEL_NAME=='Qwen3.5-35B-A3B'
assert str(common.BASE_MODEL)==config['student']['path']
assert client.DATA_MODEL==config['data_service']['model']=='Qwen3.8-27B'
return {'complete':True,'results':[],'student':common.BASE_MODEL_NAME}
sys.modules['evaluation.runner']=types.SimpleNamespace(evaluate=evaluate)
request=sys.argv[2]
sys.argv=['seed_worker',request]
runpy.run_module('controller.seed_worker',run_name='__main__')
'''
env={**os.environ,'PYTHONDONTWRITEBYTECODE':'1','DECICO_RSI_MODEL_NAME':'stale-model',
'DECICO_RSI_BASE_MODEL':'/stale/base'}
result=subprocess.run([sys.executable,'-B','-c',code,str(ROOT/'decico_rsi'),str(path)],
cwd=ROOT,env=env,text=True,capture_output=True,timeout=30)
assert result.returncode==0,result.stdout+result.stderr
assert read_json(tmp_path/'receipt.json')['status']=='complete'
assert read_json(tmp_path/'summary.json')['student']=='Qwen3.5-35B-A3B'
@pytest.mark.parametrize('parent_base',['/models/different-base',''])
def test_cross_student_adapter_is_rejected_before_loading_weights(tmp_path,parent_base):
from types import SimpleNamespace
from modeling.initialization import resolve,attach
adapter=tmp_path/'adapter';adapter.mkdir()
write_json(adapter/'adapter_config.json',{'base_model_name_or_path':parent_base})
(adapter/'adapter_model.safetensors').write_bytes(b'not-read-before-identity-check')
binding=resolve({'model':'accepted','adapter_path':str(adapter)},{'initialization':'latest_accepted'})
base=SimpleNamespace(config=SimpleNamespace(_name_or_path='/models/Qwen3.5-35B-A3B'))
with pytest.raises(ValueError,match='different base Student'):
attach(base,binding,rank=16,alpha=32,dropout=.05)
def test_other_production_students_are_rejected(config):
with pytest.raises(ValueError, match='Unsupported'):
configure(config, name='Qwen3-4B')