Vendor pinned MeZO, AGZO, LOZO and LoREN sources with independent environments. Add direct in-place fix/adjust/node training, derived history-free basis gradients, development-best checkpoints and experiment tooling. Keep models, datasets, caches and credentials outside Git. Validation: 93 project tests passed; 521 vendored author files verified.
65 lines
2.9 KiB
Python
65 lines
2.9 KiB
Python
"""Development-only selection, disk restore, and tied-weight round trip."""
|
|
from types import SimpleNamespace
|
|
import json
|
|
import pytest
|
|
import torch
|
|
from transformers import OPTConfig, OPTForCausalLM
|
|
from test_mezo_refactor import make_trainer
|
|
|
|
|
|
@torch.no_grad()
|
|
@pytest.mark.parametrize('sharded', [False, True])
|
|
def test_best_checkpoint_keeps_earliest_minimum_and_restores_weights(tmp_path, monkeypatch, sharded):
|
|
from methods.large_models import trainer as module
|
|
monkeypatch.setattr(module, 'figures', lambda *args: None)
|
|
model = OPTForCausalLM(OPTConfig(vocab_size=16, hidden_size=8, ffn_dim=16,
|
|
num_hidden_layers=1, num_attention_heads=2, max_position_embeddings=16))
|
|
engine = make_trainer(model, lambda batch: None)
|
|
engine.args.output_dir = str(tmp_path)
|
|
engine.args.select_best_checkpoint = True
|
|
engine.args.data_protocol = 'mezo'
|
|
engine.processing_class = SimpleNamespace(save_pretrained=lambda *args: None)
|
|
engine.train_dataset, engine.eval_dataset = [0], [1]
|
|
engine.curve = [];engine.started = 0
|
|
engine.progress = lambda *args: None
|
|
loss = [3.]
|
|
def evaluate(*args):
|
|
engine.counts['evaluation'] += 1
|
|
return dict(loss=loss[0], accuracy=.5)
|
|
engine.evaluate_dataset = evaluate
|
|
parameter = model.get_input_embeddings().weight
|
|
rng = torch.random.get_rng_state()
|
|
for step, value in enumerate((3., 2., 2., 4.), 1):
|
|
engine.state.global_step = step
|
|
engine.counts['training'] = step*2
|
|
parameter.fill_(float(step))
|
|
loss[0] = value
|
|
engine.measure()
|
|
assert float(parameter[0, 0]) == step # Saving does not rewind training.
|
|
assert engine.best_checkpoint['step'] == 2 # Tie retains earlier best.
|
|
assert json.loads((tmp_path/'best_checkpoint.json').read_text())['development']['loss'] == 2
|
|
if sharded:
|
|
engine.restore_best_model()
|
|
model.save_pretrained(tmp_path/'best_model', max_shard_size='1KB')
|
|
parameter.fill_(4.)
|
|
engine.restore_best_model()
|
|
assert torch.all(parameter == 2)
|
|
assert parameter is model.get_output_embeddings().weight
|
|
assert torch.equal(rng, torch.random.get_rng_state())
|
|
|
|
|
|
@torch.no_grad()
|
|
def test_initial_evaluation_is_not_a_train_checkpoint(tmp_path, monkeypatch):
|
|
from methods.large_models import trainer as module
|
|
monkeypatch.setattr(module, 'figures', lambda *args: None)
|
|
engine = module.OurTrainer.__new__(module.OurTrainer)
|
|
engine.args = SimpleNamespace(output_dir=str(tmp_path), select_best_checkpoint=True)
|
|
engine.state = SimpleNamespace(global_step=0)
|
|
engine.counts = dict(training=0, calibration=1, evaluation=0)
|
|
engine.train_dataset, engine.eval_dataset = [0], [1]
|
|
engine.curve = [];engine.started = 0;engine.progress = lambda *args: None
|
|
engine.evaluate_dataset = lambda *args: dict(loss=1.)
|
|
engine.save_model = lambda *args: (_ for _ in ()).throw(AssertionError('initial save'))
|
|
engine.measure()
|
|
assert not hasattr(engine, 'best_checkpoint')
|