modelclinic-experiment-001 / code /tests /test_vllm_hook.py
PratikBuilds's picture
Publish initial ModelClinic Experiment 001 archive
87cc7f2 verified
Raw History Blame Contribute Delete
1.28 kB
import importlib.util,sys,unittest,torch
from pathlib import Path
P=Path(__file__).resolve().parents[1]/'modal'/'vllm_long.py'; spec=importlib.util.spec_from_file_location('mc_vllm_long',P); mod=importlib.util.module_from_spec(spec);spec.loader.exec_module(mod)
class Pair(torch.nn.Module):
def __init__(self):super().__init__();self.weight=torch.nn.Parameter(torch.eye(4))
def forward(self,x):return x,None
class Block(torch.nn.Module):
def __init__(self):super().__init__();self.mlp=torch.nn.Module();self.mlp.down_proj=Pair()
class Model(torch.nn.Module):
def __init__(self):super().__init__();self.layers=torch.nn.ModuleList([Block() for _ in range(20)])
class HookTest(unittest.TestCase):
def test_constant_hook_adds_at_correct_layers_and_preserves_tuple(self):
m=Model();v=[1.,0.,0.,0.];mod.install_hooks(m,[(15,v,.25),(19,v,-.5)]);x=torch.zeros(2,4);y,b=m.layers[15].mlp.down_proj(x);z,c=m.layers[19].mlp.down_proj(x)
self.assertIsNone(b);self.assertIsNone(c);torch.testing.assert_close(y[:,0],torch.full((2,),.25));torch.testing.assert_close(z[:,0],torch.full((2,),-.5));self.assertEqual(mod.hook_stats(m)['layers.15.mlp.down_proj']['calls'],1);self.assertEqual(m.layers[14].mlp.down_proj(x)[0].sum(),0)
if __name__=='__main__':unittest.main()