code-HFD / tests /test_streaming_inference_freeze.py
Tinghong-Ye22's picture
Add files using upload-large-folder tool
5d9f970 verified
Raw History Blame Contribute Delete
990 Bytes
import ast
from pathlib import Path
import torch
def test_inference_loader_finalization_freezes_without_changing_weights():
path=Path(__file__).resolve().parents[1]/'src/psi/models/full_duplex_psi0.py'
tree=ast.parse(path.read_text())
loader=next(n for n in ast.walk(tree)
if isinstance(n,ast.FunctionDef) and n.name=='from_streaming_checkpoint')
# Execute the actual finalization expression, without loading a 3B model.
expression=loader.body[-2]
assert isinstance(expression,ast.Expr)
model=torch.nn.Sequential(torch.nn.Linear(2,2),torch.nn.Dropout(.5))
model.device=torch.device('cpu')
before={k:v.clone() for k,v in model.state_dict().items()}
exec(compile(ast.Module(body=[expression],type_ignores=[]),str(path),'exec'),{'model':model})
assert not model.training
assert not any(p.requires_grad for p in model.parameters())
for k,v in model.state_dict().items():torch.testing.assert_close(v,before[k],rtol=0,atol=0)