Add Control v2 incremental experiment results (evidence, visuals, 42 fixed samples)
a64600b verified Download method_v2/evidence/code/suite/tests/test_sd_hooks.py from CharlesZhang-USTC/snf_experiments: direct link, hf CLI and curl.
- Browser
- Download file 2.63 kB
-
https://huggingface.co/CharlesZhang-USTC/snf_experiments/resolve/main/method_v2/evidence/code/suite/tests/test_sd_hooks.py
- Command line
-
hf download hf://CharlesZhang-USTC/snf_experiments/method_v2/evidence/code/suite/tests/test_sd_hooks.py
-
curl -L -o test_sd_hooks.py https://huggingface.co/CharlesZhang-USTC/snf_experiments/resolve/main/method_v2/evidence/code/suite/tests/test_sd_hooks.py
2.63 kB
| """Check controlled flow integrates from exactly the state seen by the model.""" | |
| import unittest | |
| from unittest.mock import patch | |
| from types import SimpleNamespace | |
| import torch | |
| from suite.adapters import SD35 | |
| class DummyTransformer(torch.nn.Module): | |
| def forward(self,hidden_states,timestep):return (torch.ones_like(hidden_states)*.2,) | |
| class DummyPipe: | |
| def __init__(self): | |
| self.transformer=DummyTransformer();self.scheduler=SimpleNamespace(sigmas=torch.tensor([1.,.5,0.]),step=self.step) | |
| self.observed=[];self.integrated=[] | |
| def step(self,velocity,timestep,sample,*args,**kwargs): | |
| self.integrated.append(sample.clone());return (sample-.5*velocity,) | |
| def __call__(self,*args,**kwargs): | |
| sample=torch.ones(1,1,2,2,dtype=torch.bfloat16) | |
| for step in range(2): | |
| def capture(module,args,kw):self.observed.append(kw['hidden_states'].clone()) | |
| hook=self.transformer.register_forward_pre_hook(capture,with_kwargs=True) | |
| try:velocity=self.transformer(hidden_states=torch.cat([sample,sample]),timestep=torch.tensor([1.-step*.5]))[0][-1:] | |
| finally:hook.remove() | |
| sample=self.scheduler.step(velocity,step,sample)[0] | |
| return SimpleNamespace(images=sample) | |
| class SDHookTests(unittest.TestCase): | |
| def run_job(self,arm,alpha): | |
| model=SD35.__new__(SD35);model.cfg=dict(steps=2,resolution=2,guidance=2.);model.pipe=DummyPipe();model.decode=lambda x:x | |
| generator=torch.Generator;randn=torch.randn | |
| def cpu_gen(*args,**kw):kw['device']='cpu';return generator(*args,**kw) | |
| def cpu_randn(*args,**kw):kw['device']='cpu';return randn(*args,**kw) | |
| with patch('torch.Generator',cpu_gen),patch('torch.randn',cpu_randn),patch('suite.adapters.target_mask',return_value=torch.ones(2,2)): | |
| model.generate(dict(task=dict(prompt='x'),seed=0,alpha=alpha,arm=arm),torch.zeros(1,1,2,2,dtype=torch.bfloat16),[]) | |
| return model | |
| def test_model_and_integrator_share_pinned_state(self): | |
| model=self.run_job('local',1.) | |
| for seen,integrated in zip(model.pipe.observed,model.pipe.integrated): | |
| self.assertTrue(torch.equal(seen[-1:],integrated));self.assertEqual(seen.dtype,torch.bfloat16) | |
| def test_prediction_guide_does_not_pin_input(self): | |
| model=self.run_job('guide',1.);self.assertTrue(torch.equal(model.pipe.observed[0][-1:],torch.ones(1,1,2,2,dtype=torch.bfloat16))) | |
| def test_hooks_restore_scheduler(self): | |
| model=self.run_job('local',.5);self.assertEqual(model.pipe.scheduler.step.__self__,model.pipe) | |
| if __name__=='__main__':unittest.main() | |