"""Same quote-supervised LoRA, independent base start/full round7 continuation."""
import types

from .cluster_targets import digest
from .mask_model_training_data import PROJECT,write,require

path=PROJECT/'pallet/pallet_description_round5_vlm.py'
SOURCE=path.read_text().replace('round5','round8')
old='from .pallet_description_round4_data import PROMPT as oldprompt'
require(SOURCE.count(old)==1,'Previous VLM prompt anchor changed')
SOURCE=SOURCE.replace(old,'from .pallet_description_round7_data import PROMPT as oldprompt')
implementation=types.ModuleType('pallet._round8_vlm_training');implementation.__file__=__file__
exec(compile(SOURCE,str(path),'exec'),implementation.__dict__)
ROOT=implementation.ROOT;RUN=implementation.RUN;CONFIG=implementation.CONFIG;learning=implementation.learning
format_target=implementation.format_target;mask_spans=implementation.mask_spans;training_batch=implementation.training_batch
answer_spans=implementation.answer_spans;parse=implementation.parse;evaluate=implementation.evaluate


def train():
    require(not RUN.exists(),'Preserve round8 VLM attempt')
    try:implementation.train()
    finally:
        if RUN.exists():
            (RUN/'adapted_round8_vlm.py').write_text(SOURCE)
            write(RUN/'adapter_provenance.json',dict(adapter_sha256=digest(__file__),frozen_source_sha256=digest(path),adapted_sha256=digest(RUN/'adapted_round8_vlm.py'),hyperparameters_unchanged=True,same_prompt_as_round7=True))


def infer(mode):return implementation.infer(mode)


if __name__=='__main__':
    import argparse
    p=argparse.ArgumentParser();p.add_argument('phase',choices=('train','infer-new','infer-semantic'));a=p.parse_args()
    train() if a.phase=='train' else infer(a.phase.split('-')[1])
