Add controlled two-layer LiSA reproduction on Qwen

This commit is contained in:
2026-11-24 00:00:42 +08:00
parent 115e440455
commit 1c8943a2e8
21 changed files with 4444 additions and 7 deletions
+62
View File
@@ -0,0 +1,62 @@
"""Check exact epoch resume through the actual LiSA training entry point."""
import json
from pathlib import Path
from tempfile import TemporaryDirectory
import unittest
from unittest.mock import patch
import numpy as np
import torch
from crossqkv.lisa_effect import train
from functional.model import PREFIX
from functional.reference_check import ArrayLM
from tests.test_lisa_tail import Tensor
class TrainingResumeTests(unittest.TestCase):
def test_checkpoint_resume_preserves_all_selected_outputs(self):
rng = np.random.default_rng(22)
def rand(shape):
return (.13*rng.standard_normal(shape)).astype('f4')
torch.set_num_threads(1)
c = {'rms_norm_eps': 1e-6, 'layer_types': ['full_attention'], 'hidden_size': 8,
'num_attention_heads': 2, 'num_key_value_heads': 1, 'head_dim': 8,
'rope_parameters': {'rope_theta': 10000., 'partial_rotary_factor': .5}}
shapes = {'input_layernorm': (8,), 'post_attention_layernorm': (8,),
'self_attn.q_proj': (32, 8), 'self_attn.k_proj': (8, 8),
'self_attn.v_proj': (8, 8), 'self_attn.o_proj': (8, 16),
'self_attn.q_norm': (8,), 'self_attn.k_norm': (8,),
'mlp.gate_proj': (13, 8), 'mlp.up_proj': (13, 8), 'mlp.down_proj': (8, 13)}
weights = {f'layers.0.{k}.weight': rand(v) for k, v in shapes.items()}
weights.update({'embed_tokens.weight': rand((12,8)), 'norm.weight': rand((8,))})
model = ArrayLM(weights, c)
model.tensors = {PREFIX+k: Tensor(v) for k,v in weights.items()}
with TemporaryDirectory() as directory:
p = Path(directory)
cache, out = p/'cache', p/'out'
cache.mkdir()
out.mkdir()
(p/'model_config.json').write_text(json.dumps(c))
(cache/'selection.json').write_text(json.dumps({'sites':[0], 'task':'fixture',
'source_sha256':{}}))
labels = np.arange(1, 5, dtype=np.int64)
for split, count in [('fit',12), ('validation',12), ('test',32)]:
for i in range(0, count, 4):
ids = rng.integers(1, 12, size=(4, 4))
gold = rng.integers(0, 4, size=4)
ids[:, -1] = labels[gold]
expected, _ = model.forward(ids)
np.savez(cache/f'{split}-{i:03}.npz', ids=ids, lengths=np.full(4,4),
labels=labels, gold=gold, residual=model.embedding(ids),
donor=rand((4,2,4,4)), expected=expected,
names=[f'{split}-{k}' for k in range(i,i+4)])
with patch('functional.model.StreamingLM', return_value=model), patch('builtins.print'):
train(p, cache, out, 1)
before = (out/'test-items.json').read_bytes()
lines = (out/'trace.json').read_bytes()
train(p, cache, out, 1, resume=True)
self.assertEqual(before, (out/'test-items.json').read_bytes())
self.assertEqual(lines, (out/'trace.json').read_bytes())